从位图转换为uri

时间:2019-11-05 15:04:04

标签: android bitmap uri

我试图将位图转换为uri,但是应用程序崩溃了。

我尝试了许多在此处发布的代码,但是没有一个对我有用。

Bitmap photo = (Bitmap) data.getExtras().get("data");

Uri tempUri = getImageUri(this,photo);

public Uri getImageUri(Context inContext, Bitmap inImage) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    String path = 
     MediaStore.Images.Media.insertImage(inContext.getContentResolver(), 
     inImage, 
     "Title", null);
      return Uri.parse(path);
}

这是我得到的错误:

  

E / Android运行时:致命异常:主要       进程:net.simplifiedlearning.firebaseauth,PID:28469       java.lang.RuntimeException:无法将结果ResultInfo {who = null,request = 1888,result = -1,data = Intent {act = inline-data(has extras}}}传递给活动{net.simplifiedlearning.firebaseauth / net。简化的学习.firebaseauth.UploadProductActivity}:java.lang.NullPointerException:uriString           在android.app.ActivityThread.deliverResults(ActivityThread.java:4496)           在android.app.ActivityThread.handleSendResult(ActivityThread.java:4538)           在android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:49)           在android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)           在android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)           在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1906)           在android.os.Handler.dispatchMessage(Handler.java:106)           在android.os.Looper.loop(Looper.java:193)           在android.app.ActivityThread.main(ActivityThread.java:6863)           在java.lang.reflect.Method.invoke(本机方法)           在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:537)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)        引起原因:java.lang.NullPointerException:uriString           在android.net.Uri $ StringUri(Uri.java:490)           在android.net.Uri $ StringUri(Uri.java:480)           在android.net.Uri.parse(Uri.java:452)           在net.simplifiedlearning.firebaseauth.UploadProductActivity.getImageUri(UploadProductActivity.java:404)           在net.simplifiedlearning.firebaseauth.UploadProductActivity.onActivityResult(UploadProductActivity.java:390)           在android.app.Activity.dispatchActivityResult(Activity.java:7476)           在android.app.ActivityThread.deliverResults(ActivityThread.java:4489)           在android.app.ActivityThread.handleSendResult(ActivityThread.java:4538)           在android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:49)           在android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)           在android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)           在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1906)           在android.os.Handler.dispatchMessage(Handler.java:106)           在android.os.Looper.loop(Looper.java:193)           在android.app.ActivityThread.main(ActivityThread.java:6863)           在java.lang.reflect.Method.invoke(本机方法)           在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:537)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)   D / OSTracker:OS事件:崩溃   D / AbstractTracker:活动成功   进程28469终止。

谢谢

1 个答案:

答案 0 :(得分:0)

无法发表评论,因为我也是新来的人,所以我将其输入答案

签出此代码-> BitmapIntoUri