2016-12-16 78 views
1

我的應用需要將圖片上傳到firebase存儲並將其顯示在recyclerView中。我的前兩個圖像顯示在列表中。但是,當我嘗試上傳第三張圖片時,應用程序崩潰,並且無法將圖像存儲在firebase上。每個圖像大小從500KB到700KB不等。我也在我的清單中使用了android:largeHeap =「true」。該怎麼辦?上傳圖片時Firebase應用崩潰

//下面的代碼:

public class PostActivity extends Activity { 


private EditText postTitle; 
private EditText postDescription; 
private Button submitButton; 
private Uri imageUri=null; 
private StorageReference storage; 
private ProgressDialog progressDialogue; 
private DatabaseReference database; 
public String titleVal; 
public String descriptionVal; 
private ImageView mImageView; 
private static final int GALLERY_REQUEST=1; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_post); 

    storage= FirebaseStorage.getInstance().getReference(); 
    database= FirebaseDatabase.getInstance().getReference().child("Posts"); 





    mImageView=(ImageView) findViewById(R.id.imageView); 

    postTitle=(EditText) findViewById(R.id.titleField); 
    postDescription=(EditText)findViewById(R.id.descriptionField); 
    submitButton=(Button) findViewById(R.id.submitPost); 
    progressDialogue=new ProgressDialog(this); 


    imageUri = Uri.parse(getIntent().getStringExtra("imageUri")); 
    ImageView img = (ImageView) findViewById(R.id.imageView); 
    img.setImageURI(imageUri); 

//職位是SUBMITED後會發生什麼。

submitButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      startPosting(); 

     } 
    }); 
} 

@Override 
public void onSaveInstanceState(Bundle outState) { 
    super.onSaveInstanceState(outState); 
    outState.putString("titleVal", titleVal); 
    outState.putString("descriptionVal",descriptionVal); 

} 


private void startPosting() { 


    titleVal=postTitle.getText().toString().trim(); 
    descriptionVal=postDescription.getText().toString().trim(); 



    if(!TextUtils.isEmpty(titleVal) && !TextUtils.isEmpty(descriptionVal) && imageUri!=null){ 
     progressDialogue.setMessage("আপলোড হচ্ছে......"); 
     progressDialogue.show(); 

     StorageReference filePath=storage.child("post_images").child(imageUri.getLastPathSegment()); 
     filePath.putFile(imageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() { 
      @Override 
      public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) { 
       Uri downloadUrl=taskSnapshot.getDownloadUrl(); 
       Picasso.with(PostActivity.this).load(downloadUrl).into(mImageView); 
       DatabaseReference newPost=database.push(); 
       newPost.child("title").setValue(titleVal); 
       newPost.child("description").setValue(descriptionVal); 
       newPost.child("image").setValue(downloadUrl.toString()); 





       progressDialogue.dismiss(); 
       Toast.makeText(getApplicationContext(), "Successfully Uploaded", Toast.LENGTH_LONG).show(); 

       startActivity(new Intent(PostActivity.this,HomeActivity.class)); 
      } 
     }); 

    }else{ 
     Toast.makeText(getApplicationContext(), "Please provide a Title", Toast.LENGTH_LONG).show(); 
    } 


    } 

} 
+2

你的錯誤日誌說什麼? – Piyush

+0

如何將圖像存儲在Firebase中?例如:Base64字符串或內置文件上傳方法 – R2R

+0

我正在使用字符串。我也應該在這裏粘貼代碼。我不是嗎?現在可以粘貼嗎? –

回答

2

試試下面的代碼,我使用這個代碼上傳圖像火力存儲,讓你發現任何問題,或者有任何疑問

Uploading image to firebase storage After uploading image to firebase storage

見下圖我知道,您可以看到圖像已成功上傳 Firebase storage image

1 - 在您的Java文件,把這些代碼

private SimpleDraweeView imgProfile; 
private FirebaseStorage storage; 
private Uri file, resultUri; 
private StorageMetadata metadata; 
private UploadTask uploadTask; 
private StorageReference storageRef; 
private final String MY_BUCKET_PATH = "YOUR BUCKET PATH OF YOUR STORAGE"; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_user_profile); 

    storage = FirebaseStorage.getInstance(); 
    storageRef = storage.getReferenceFromUrl(MY_BUCKET_PATH); 
} 

2 - 在onActivityResult上傳圖片獲取圖像的URI來火力存儲,還要確保你得到正確的圖像後烏里

private void UploadImage() { 

    file = Uri.fromFile(new File(resultUri.getPath())); 

    // Create the file metadata 
    metadata = new StorageMetadata.Builder().setContentType("image/jpg").build(); 

    // Upload file and metadata to the path 'images/mountains.jpg' 
    uploadTask = storageRef.child("images/" + file.getLastPathSegment()).putFile(file, metadata); 

    // Listen for state changes, errors, and completion of the upload. 
    uploadTask.addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() { 
     @Override 
     public void onProgress(UploadTask.TaskSnapshot taskSnapshot) { 
      double progress = (100.0 * taskSnapshot.getBytesTransferred())/taskSnapshot.getTotalByteCount(); 
      showUploadProgress(progress); 
      System.out.println("Upload is " + progress + "% done"); 
     } 
    }).addOnPausedListener(new OnPausedListener<UploadTask.TaskSnapshot>() { 
     @Override 
     public void onPaused(UploadTask.TaskSnapshot taskSnapshot) { 
      System.out.println("Upload is paused"); 
     } 
    }).addOnFailureListener(new OnFailureListener() { 
     @Override 
     public void onFailure(@NonNull Exception exception) { 
      // Handle unsuccessful uploads 
     } 
    }).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() { 
     @Override 
     public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) { 
      Log.e("Uploading image==", "onSuccess"); 
      // Handle successful uploads on complete 
      Uri downloadUrl = taskSnapshot.getMetadata().getDownloadUrl(); 
      imgProfile.setImageURI(Uri.parse(downloadUrl.toString())); // Here image will be reflected after uploading 
      mDialog.dismiss(); 
      Log.e("DownloadUrl getPath==>>", taskSnapshot.getDownloadUrl().getPath()); 
      Log.e("Metadata getPath==>>", taskSnapshot.getMetadata().getPath()); // this line will give you path to download Image from Firebase storage omitting child storage reference 
      Log.e("Metadata dwndUrl getpath", taskSnapshot.getMetadata().getDownloadUrl().getPath()); 
      Log.e("storageRef path==>>", taskSnapshot.getMetadata().getReference().getPath()); 
      mSessionManager.storeStringValues("profile_photo", downloadUrl.toString()); 
      mSessionManager.storeStringValues("profile_photo_path", taskSnapshot.getMetadata().getPath()); 
     } 
    }); 
} 
相關問題