Rohit Malish
Rohit Malish

Reputation: 3229

Bitmap is null after loading it from externalStorage

In my reference activity I create bitmap from user selection using this code:

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;
}

and then in my other activity I try to retrieve this cropped image and draw it. but it always appears as null and default bitmap gets drawn.

Here is the retrieving part:

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();

Upvotes: 1

Views: 378

Answers (1)

Aldridge1991
Aldridge1991

Reputation: 1367

Ten minutes ago i was having the same issue until i found this:

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

And in you result method add this:

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);
    } 
}  

Hope this helps.

Upvotes: 1

Related Questions