2017-12-14 15 views
0

Firebase Storageバケットに画像をアップロードしますが、常にOnFailureListenerから値を返します。FirebaseStorageは画像をアップロードしません

ここでは、コードです:ここで

public class AddProductFragment extends Fragment { 
    private static final int IMAGE_REQUEST = 1; 

    private ProgressDialog progressDialog; 
    private Uri uri; 
    private EditText mTitle,mPrice; 
    private Button mBtn,mUpload; 
    private ImageView mImage; 
    private FirebaseStorage mStorageRef; 
    private StorageReference mRef; 

    public AddProductFragment() { 
     mStorageRef = FirebaseStorage.getInstance(); 
     mRef = mStorageRef.getReference().child("images"); 
    } 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
     View v = inflater.inflate(R.layout.add_product_fragment,container,false); 
     find(v); 
     onClick(); 
     return v; 
    } 

    private void find(View v) { 
     mTitle = (EditText)v.findViewById(R.id.singleProductTitle); 
     mPrice = (EditText)v.findViewById(R.id.singleProductPrice); 
     mBtn = (Button) v.findViewById(R.id.addProductSelectImageBtn); 
     mImage = (ImageView)v.findViewById(R.id.addProductImage); 
     mUpload = (Button)v.findViewById(R.id.uploadBtn); 
    } 

    private void onClick() { 
     mBtn.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); 
       intent.addCategory(Intent.CATEGORY_OPENABLE); 
       intent.setType("image/*"); 
       startActivityForResult(Intent.createChooser(intent,"Select Picture"), IMAGE_REQUEST); 

      } 
     }); 

     mUpload.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       progressDialog = new ProgressDialog(getActivity()); 
       progressDialog.setMax(100); 
       progressDialog.setMessage("Uploading..."); 
       progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
       progressDialog.show(); 
       progressDialog.setCancelable(false); 

       String title = mTitle.getText().toString(); 
       String price = mPrice.getText().toString(); 
       if (!title.isEmpty() && !price.isEmpty()) { 
        Product product = new Product(); 
        Uri file = Uri.fromFile(new File(getRealPathFromURI(uri))); 
        mRef.child("original").child(product.getStringId()); 

        mRef.putFile(file).addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() { 
         @Override 
         public void onProgress(UploadTask.TaskSnapshot taskSnapshot) { 
          double progress = (100.0 * taskSnapshot.getBytesTransferred())/taskSnapshot.getTotalByteCount(); 
          //sets and increments value of progressbar 
          progressDialog.incrementProgressBy((int) progress); 
         } 
        }).addOnFailureListener(new OnFailureListener() { 
         @Override 
         public void onFailure(@NonNull Exception e) { 
          Toast.makeText(getActivity(), "Error uploading....", Toast.LENGTH_SHORT).show(); 
          progressDialog.dismiss(); 
         } 
        }).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() { 
         @Override 
         public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) { 
          Uri downloadUrl = taskSnapshot.getDownloadUrl(); 
          Toast.makeText(getActivity(),"Upload successful",Toast.LENGTH_SHORT).show(); 
          //TODO: redirect to the uploaded product; 
          progressDialog.dismiss(); 
         } 
        }); 



       } 
      } 
     }); 
    } 

    @Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if(requestCode == IMAGE_REQUEST && resultCode == RESULT_OK) { 
      try { 
       uri = data.getData(); 
       Bitmap bitmap = null; 
       if(uri != null) { 
        try { 
         bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), uri); 
        } catch (IOException e) { 
         e.printStackTrace(); 
        } 
        mImage.setImageBitmap(bitmap); 
       } else { 
        Toast.makeText(getActivity(), "Uri not found...", Toast.LENGTH_SHORT).show(); 
       } 
      } catch (NullPointerException e) { 
       e.getMessage(); 
      } 

     } 
    } 

    private String getRealPathFromURI(Uri contentURI) { 

     String thePath = "no-path-found"; 
     String[] filePathColumn = {MediaStore.Images.Media.DISPLAY_NAME}; 
     Cursor cursor = getActivity().getContentResolver().query(contentURI, filePathColumn, null, null, null); 
     if(cursor.moveToFirst()){ 
      int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
      thePath = cursor.getString(columnIndex); 
     } 
     cursor.close(); 
     return thePath; 
    } 


} 

は例外です:

/com.example.cmd.pop E/StorageException: は/画像-0-02- 05-64dcdbd29d607a4421b258b2adbfb848cf3262ee096a8b0ab2dbecd22631feea-V.jpg: オープンに失敗しました:ENOENT(そのようなファイルまたはディレクトリはありません)

java.io.FileNotFoundException: /image-0-02-05-64dcdbd29d607a4421b258b2adbfb848cf3262ee096a8b0ab2dbecd22631feea-V.jpg: オープンに失敗しました:ENOENT(そのようなファイルまたはディレクトリ)

+0

を明示するためのアクセス許可を追加しましたまあ、OnFailureListenerは例外を与えている - あなたは完全にあなたのコード内で無視していること。あなたはその例外を見ましたか?私はそれが何が間違っているかについて有益な情報を与えると期待しています。 –

+0

@JonSkeet投稿を更新しました。 – Kristjan

+0

それは、あなたが調査しなければならないようなものです。ローカルファイルが見つからない場合、Firebaseの問題ではありません。 –

答えて

0

は、とにかく、私は解決策を見つけ、参照パスが間違っていた

コンストラクタの変更

public AddProductFragment() { 
    mStorageRef = FirebaseStorage.getInstance(); 
    mRef = mStorageRef.getReference().child("images/original/"); 
    mThumbRef = mStorageRef.getReference().child("images/thumb"); 
} 

OnActivityResult

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == IMAGE_REQUEST && resultCode == RESULT_OK) { 
      uri = data.getData(); 

      String title = mTitle.getText().toString(); 
      String price = mPrice.getText().toString(); 
      if (!title.isEmpty() && !price.isEmpty()) { 
       Product product = new Product(); 
       StorageReference thumbRef = mRef.child(product.getStringId()).child(uri.getLastPathSegment()); 
       StorageReference imageRef = mRef.child(product.getStringId()).child(uri.getLastPathSegment()); 

       imageRef.putFile(uri).addOnFailureListener(new OnFailureListener() { 
        @Override 
        public void onFailure(@NonNull Exception e) { 
         Toast.makeText(getActivity(), e.getMessage(), Toast.LENGTH_SHORT).show(); 
         Log.d(TAG, e.getMessage()); 
        } 
       }).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() { 
        @Override 
        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) { 
         Uri downloadUrl = taskSnapshot.getDownloadUrl(); 
         Toast.makeText(getActivity(), "Upload successful", Toast.LENGTH_SHORT).show(); 
         //TODO: redirect to the uploaded product; 
        } 
       }); 

       thumbRef.putFile(uri).addOnFailureListener(new OnFailureListener() { 
        @Override 
        public void onFailure(@NonNull Exception e) { 
         Toast.makeText(getActivity(), e.getMessage(), Toast.LENGTH_SHORT).show(); 
         Log.d(TAG, e.getMessage()); 
        } 
       }).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() { 
        @Override 
        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) { 
         Uri downloadUrl = taskSnapshot.getDownloadUrl(); 
         Toast.makeText(getActivity(), "Upload successful", Toast.LENGTH_SHORT).show(); 
         //TODO: redirect to the uploaded product; 
        } 
       }); 




      } 

     } 
} 

そして

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> 
関連する問題