Question

UPDATE!!

So Lokesh put me on the right path, and showed me that the problem was the size of the file being too large to show in the imageview. I was able to fix the imageview preview problem with the following code under my onActivityResult:

try {
                    Bitmap picture = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory().getPath()+"/td01.png");
                    int nh = (int) ( picture.getHeight() * (512.0 / picture.getWidth()) );
                    Bitmap scaled = Bitmap.createScaledBitmap(picture, 512, nh, true);
                    Log.v("Path", Environment.getExternalStorageDirectory().getPath()+"/td01.png");    
                    pic1.setImageBitmap(scaled);
                } catch (Exception e) {
                    Log.e("Error reading file", e.toString());
                }

Thanks Lokesh!

----------------- ORIGINAL ISSUE BELOW THIS LINE -------------------

So I'm trying to both save an image to the SD card for use later, AND display the saved image in an imageview which also serves as the button which takes the photo. Here's the code:

  pic1.setOnClickListener(new OnClickListener() {
                  public void onClick(View v) {
                      Intent camera_intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                      File image1 = new File(Environment.getExternalStorageDirectory(),"td01.png");
                      camera_intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(image1));
                      startActivityForResult(camera_intent, CAMERA_PIC_REQUEST1);
                              }
                });

followed by:

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch(requestCode){
        case 1:
            if(resultCode==RESULT_OK){
                  Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
                  pic1.setImageBitmap(thumbnail);
                }
        }

Now, if I remove the following code from the onclick, it shows the thumbnail as I expect it to:

File image1 = new File(Environment.getExternalStorageDirectory(),"td01.png");
camera_intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(image1));

...but without that code, it doesn't save the file to my sd card.

The problem is, if I don't remove that code, it saves the image to my SD Card but immediately crashes before returning to the activity after tapping SAVE in the camera activity, unless I remove the following code from my onActivityResult:

Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
pic1.setImageBitmap(thumbnail);

I've also tried many variations of the following code in my onActivityResult, hoping to display it from the actual file in a different way, but it never works and only shows a blank imageview, but at least in that case the crash doesn't occur, because I removed the get extras get data code:

Bitmap photo1 = BitmapFactory.decodeFile("/sdcard/td01.png");
pic1.setImageBitmap(photo1);

I've been struggling with this for days and am at a loss here. Hope someone can show me what stupid thing I'm doing wrong and explain why this isn't working.

Thanks!

Was it helpful?

Solution

Try this:

try {
            Bitmap picture = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory().getPath()+"/DCIM/MyPhoto.jpg");
            Log.v("Path", Environment.getExternalStorageDirectory().getPath()+"/DCIM/MyPhoto.jpg");
            mImageView.setImageBitmap(picture);
        } catch (Exception e) {
            Log.e("Error reading file", e.toString());
        }

OTHER TIPS

do NOT access the SD card directly, try accessing it trough Environment Like this

String imageDir = Environment.getExternalStorageDirectory()+"/apple.jpg";

and then you can call BitmapFactory:

Bitmap myBitmap = BitmapFactory.decodeFile(imageDir);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top