从externalStorage加载后,位图为空

时间:2012-09-01 16:01:18

标签: android bitmap

在我的参考活动中,我使用以下代码从用户选择创建位图:

Uri selectedImage = imageReturnedIntent.getData(); // uri from user selection.
File tempFile; // temporary File
Uri tempUri; // temporary Uri

try 
{
    // initializing File
    tempFile = File.createTempFile("crop", "png", Environment.getExternalStorageDirectory());
}
catch (IOException e1)
{
    tempFile = null;
}

if(tempFile != null)
{
    // initializing Uri
    tempUri = Uri.fromFile(tempFile);   
}
else
{
    tempUri = selectedImage;
}

// creating new crop intent
final Intent intent = new Intent("com.android.camera.action.CROP");
intent.setData(selectedImage); // original image Uri
intent.putExtra("outputX", width);
intent.putExtra("outputY", height);
intent.putExtra("aspectX", width);
intent.putExtra("aspectY", height);
intent.putExtra("scale", true);
intent.putExtra("noFaceDetection", true);
intent.putExtra("output", tempUri);  // cropped image Uri
intent.putExtra("outputFormat", "PNG");
startActivityForResult(intent, 23);

InputStream imageStream;
Bitmap yourSelectedImage;
try
{
    // initializing InputStream
    imageStream = getContentResolver().openInputStream(tempUri);
}
catch (FileNotFoundException e) 
{
    imageStream = null;
}

if(imageStream != null)
{
    // getting Bitmap from Uri
    yourSelectedImage = BitmapFactory.decodeStream(imageStream);
}
else
{
    yourSelectedImage = null;
}

然后在我的其他活动中,我尝试检索此裁剪的图像并绘制它。但它总是显示为null并且会绘制默认位图。

这是检索部分:

File tempF = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "crop.png"); // also tried using only "crop" as filename.

Uri uri = Uri.fromFile(tempF);

InputStream imageStream;
Bitmap yourSelectedImage;
try
{
imageStream = getContentResolver().openInputStream(uri);
}
catch (FileNotFoundException e) 
{
    imageStream = null;
}

if(imageStream != null)
{
    yourSelectedImage = BitmapFactory.decodeStream(imageStream);
}
else
{
    yourSelectedImage = null;
}

if(yourSelectedImage == null)
{
    drawable = BitmapFactory.decodeResource(getResources(),R.drawable.bg);  // bg is default bitmap if image is not found.
}
else
{
    drawable = yourSelectedImage;
}

// *all drawing happens here*

tempF.delete();

1 个答案:

答案 0 :(得分:1)

十分钟前我遇到了同样的问题,直到我发现了这个问题:

Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
             startActivityForResult(intent, TFRequestCodes);

在你的结果方法中添加:

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 

if (requestCode == TFRequestCodes && resultCode == RESULT_OK) {  

        selectedImage = data.getData();
        String[] filePathColumn = {MediaStore.Images.Media.DATA};

        Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        filePath = cursor.getString(columnIndex);
        cursor.close();

       thumbnail=decodeSampledBitmapFromResource(getResources(), filePath,
                120, 85);


        i1.setImageBitmap(thumbnail);
    } 
}  

希望这有帮助。