Android:来自getBitmap的库崩溃问题的Java Image Picker

时间:2017-03-08 08:20:54

标签: android image bitmap gallery picker

我跟着这个帖子 https://stackoverflow.com/a/10168114/5078763

http://www.theappguruz.com/blog/android-take-photo-camera-gallery-code-sample
编辑了如下代码

我的应用程序崩溃了

MediaStore.Images.Media.getBitmap

执行低于Bfr Log

Log.d("TAG", "onActivityResult: bfr");

但不是Afr Log

Log.d("TAG", "onActivityResult: Afr");

我的完成代码如下:

void calGlryImjPkrFnc()
{
    Intent glrImjPkrVar = new Intent(Intent.ACTION_PICK,
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    glrImjPkrVar.setType("image/*");
    startActivityForResult(glrImjPkrVar , 1);
}

void calCamImjPkrFnc()
{
    Intent camImjPkrVar = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(camImjPkrVar, 2);
}

protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent)
{
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent);

    Bitmap pkdImjBitmapVar = null;

    if(requestCode == 1 && resultCode == RESULT_OK)
    {
        Log.d("TAG",requestCode + "");
        if (imageReturnedIntent != null)
        {
            try
            {

                ContentResolver contntMgrVaj = getApplicationContext().getContentResolver();
                Uri uriVaj = imageReturnedIntent.getData();

                Log.d("TAG", "onActivityResult: bfr");

                pkdImjBitmapVar = MediaStore.Images.Media.getBitmap(contntMgrVaj, uriVaj);

                Log.d("TAG", "onActivityResult: Afr");

            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }
    else if(requestCode == 2 && resultCode == RESULT_OK)
    {
        pkdImjBitmapVar = (Bitmap) imageReturnedIntent.getExtras().get("data");
    }

    imjVyuVar.setImageBitmap(pkdImjBitmapVar);
}

帮我修复此崩溃

2 个答案:

答案 0 :(得分:1)

首先发布完整的崩溃日志。

但是,请检查uriVaj是否为空以及您的应用程序权限。

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

答案 1 :(得分:0)

void calCamImjPkrFnc() {
    File image = new File("Your path you want to save file");
    Uri uriSavedImage = Uri.fromFile(image);
    Intent camImjPkrVar = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); 
    intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, uriSavedImage); 
    startActivityForResult(camImjPkrVar, 2);
}

通过添加“android.provider.MediaStore.EXTRA_OUTPUT”为我工作,希望它对你有所帮助。