将文件存储到外部存储需要花费太多时间

时间:2019-06-03 06:10:43

标签: android android-bitmap bitmapfactory

我正在尝试使用本地相机从我的应用中拍照。将图像保存在上述文件夹中花费的时间太长。由于这个时间滞后,我在使用Bitmap库的代码中得到了NullPointerException。

我已经检查了与该问题相关的其他答案,我在AndroidManifest.xml中提供了权限,还提供了Easy权限-实现“ pub.devrel:easypermissions:0.2.1”。我可以将图像存储在外部,但是5-10分钟后图像会显示在文件夹中。然后,由于无法在设备上找到图像,因此代码无法读取图像。应用崩溃后,它会显示出来。

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {

        Bitmap photo = BitmapFactory.decodeFile(f.getAbsolutePath());
        imageView.setImageBitmap(photo);

        Intent i = new Intent(Intent.ACTION_SEND);
        Uri uri = Uri.fromFile(f);
        i.putExtra(Intent.EXTRA_STREAM, uri);
        try {
            startActivity(Intent.createChooser(i, "Send mail..."));
        } catch (android.content.ActivityNotFoundException ex) {
            Toast.makeText(HomeScreen.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
        }
        i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        i.setType("message/rfc822");
        i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"abc@gmail.com"});
        i.putExtra(Intent.EXTRA_SUBJECT, "Subject");
        i.putExtra(Intent.EXTRA_TEXT   , "body of email");


    }
  }

此行显示错误       imageView.setImageBitmap(photo);

D/AndroidRuntime: Shutting down VM
 java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1888, result=-1, data=Intent { dat=file:///storage/emulated/0/Pictures/pic/image-9747.jpg flg=0x1 }} to activity {com.example.pic/com.example.pic.HomeScreen}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageBitmap(android.graphics.Bitmap)' on a null object reference
     at android.app.ActivityThread.deliverResults(ActivityThread.java:3779)
     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3822)
     at android.app.ActivityThread.access$1400(ActivityThread.java:155)
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1429)
     at android.os.Handler.dispatchMessage(Handler.java:102)
     at android.os.Looper.loop(Looper.java:152)
     at android.app.ActivityThread.main(ActivityThread.java:5497)
     at java.lang.reflect.Method.invoke(Native Method)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageBitmap(android.graphics.Bitmap)' on a null object reference```

1 个答案:

答案 0 :(得分:0)

您是否已将此内容添加到onCreate()

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

ImageView上的示例Manifest.xml声明

 <ImageView
  android:id="@+id/uncheck_disp_act"
  android:layout_width="25dp"
  android:layout_height="25dp"
  app:srcCompat="@drawable/uncheck" />