我已经解决了与活动结果有关的大多数相关问题,这些结果使用相机Intent返回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();
}
}
现在的问题是,每当我尝试打开相机并获取一些照片时,照片也会被拍摄,并且它也位于内存中,但是活动不会向我返回文件路径。该测试设备的内存不足。但是,只需单击1或2次,它就可以使用相机正常工作,但是超过2次,它会崩溃并显示此错误:
无法恢复活动java.lang.RuntimeException:失败 传递结果ResultInfo {who = null,request = 2,result = -1, data = null}到活动java.lang.NullPointerException 在 android.app.ActivityThread.performResumeActivity(ActivityThread.java:2996) 在 android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3025) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2396) 在 android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3974) 在android.app.ActivityThread.access $ 1000(ActivityThread.java:166) 在 android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1287)
由于图像可能很大,因此我调整了图像的大小并设置了largeheap=true
,但不幸的是,它没有用。谁能指导我做错了什么地方?
答案 0 :(得分:1)
您的进程很可能在后台终止。您需要在已保存实例状态Bundle
中保留文件位置,以便在需要时可以将其取回。
This sample project(来自this book)说明了如何使用保存的实例状态Bundle
来保持文件位置。它还演示了如何使用FileProvider
,如果您希望自己的应用在Android 7.0+设备上运行,则需要Uri.fromFile()
will not work。
答案 1 :(得分:0)
如果用户取消请求,则数据将以NULL返回。当您调用data.getExtras().get("data");
时,线程将引发nullPointerException。我认为您只需要添加一个条件来检查返回的数据是否为空。