使用相机捕获图像,文件已创建但未在 ActivityResult 中返回

Capture Image using Camera, File created but not returning in ActivityResult

我已经解决了大多数关于使用相机 Intent activity结果 returning null 的相关问题。下面是我的代码:

String filepath;
File file;
Bitmap bitmap;
int previewcount=0;
@OnClick(R.id.lnrcamera)
public void opencamera()
{
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (intent.resolveActivity(this.getPackageManager()) != null)
    {
        try
        {
            file = createImageFile();
        }
        catch (IOException ex)
        {
            // Error occurred while creating the File
        }
        // Continue only if the File was successfully created
        if (file != null)
        {
            intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
            startActivityForResult(intent, FILE_CAMERA);
        }
    }
}

private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
            imageFileName,  // prefix
            ".jpg",         // suffix
            storageDir      // directory
    );

    // Save a file: path for use with ACTION_VIEW intents
    filepath = "file:" + image.getAbsolutePath();
    return image;
}

在 ActivityResult 上

 else if (requestCode==FILE_CAMERA && resultCode == RESULT_OK)
    {
        try
        {
            Log.d("sdfasdfasdf", "onActivityResult: "+filepath.isEmpty());
            bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.parse(filepath));
            int newHeight = (bitmap.getHeight() * 500)/bitmap.getWidth();
            bitmap = Bitmap.createScaledBitmap(bitmap, 500, newHeight, false);
            list_bitmap.add(bitmap);
            lnrpreview.setVisibility(View.VISIBLE);
            Adapter_Image adapter_expenseCategory=new Adapter_Image(this, 0, list_bitmap, new RecyclerCallback() {
                @Override
                public void onPreviewDelete(List<Bitmap> list_bitmap)
                {
                    Log.d("hereornot", "onPreviewDelete: "+list_bitmap.size());
                    previewcount--;
                    if (list_bitmap.size()==0)
                    {
                        lnrpreview.setVisibility(View.GONE);
                        imgpreviewcount.setImageResource(R.drawable.flagoff);
                    }
                    btxtpreviewcount.setBadgeCount(previewcount);
                }
            });
            recycler.setAdapter(adapter_expenseCategory);
            previewcount++;
            btxtpreviewcount.setBadgeCount(previewcount);
            imgpreviewcount.setImageResource(R.drawable.flagon);
        }
        catch (OutOfMemoryError e)
        {
            Toast.makeText(Activity_NewExpense.this, "Your device is out of Memory. Please free up some space.", Toast.LENGTH_SHORT).show();
        }
        catch (IndexOutOfBoundsException e)
        {
            Log.d("hereornot", "onPreviewDelete: "+e.getMessage());
        }
        catch (FileNotFoundException e)
        {
            Log.d("hereornot", "onPreviewDelete: "+e.getMessage());
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

现在的问题是,每当我尝试打开相机并获取一些照片时,照片都会被拍下来,而且它也在内存中,但是 activity 不会 return 给我文件路径。测试设备在 memory.However 上 运行 低,点击一两次它可以使用相机正常工作,但点击两次以上它会崩溃并显示此错误:

Unable to resume activity java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, result=-1, data=null} to activity java.lang.NullPointerException at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2996) at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3025) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2396) at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3974) at android.app.ActivityThread.access00(ActivityThread.java:166) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1287)

由于图像可能很大,我调整了它的大小并设置了 largeheap=true 但不幸的是它没有用。谁能指导我哪里做错了?

如果用户取消请求,数据将返回为NULL。当您调用 data.getExtras().get("data"); 时,线程将抛出 nullPointerException。我想你只需要添加一个条件来检查返回的数据是否为空。

很有可能,您的进程在后台时正在终止。您需要在保存的实例状态 Bundle 中保留您的文件位置,以便在需要时取回它。

This sample project (from this book) illustrates how to use the saved instance state Bundle to hold onto the file location. It also demonstrates how to use FileProvider, which you will need if you want your app to work on Android 7.0+ devices, as Uri.fromFile() will not work.