dylan murphy
dylan murphy

Reputation: 1370

using ACTION_IMAGE_CAPTURE to take picture and setImageBitmap to display it

EDIT i updated the code to reflect changes suggested in both answers, unfortunately, now my app force closes. the error is listed at the bottom

this is my camera/picture class in its entirety (except for imports) this class is supposed to take a picture, display it to the screen, and let another class have the string path of the picture for use as an attachment. it takes the picture fine, it emails the picture fine, but i cannot figure out how to make it display the picture after its taken. i know there is some sloppy code in there (like in the the onActivityResult) but everything except

Bitmap bm = BitmapFactory.decodeFile(image_string);
imageview.setImageBitmap(bm);

works great, so I am very interested in finding a solution. any other advice you have on how to clean this up would also be more than welcome, this was just sort of hobbled together from multiple different sources.

public class LooksLike extends Activity
    {       
    Button camera;
    Intent intent;
    static String image_string;
    ImageView imageview;
    public void onCreate (Bundle savedInstanceState)
        {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lookslike);
        camera = (Button) findViewById(R.id.camera);
        imageview = (ImageView) findViewById(R.id.debris_view);
        camera.setOnClickListener(new View.OnClickListener()
            {
            public void onClick(View v)
                {
                takePhoto();
                }
            });
        }

    public void takePhoto()
        {
        intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(getTempFile(this)) ); 
        startActivityForResult(intent, 1);
        Bitmap bm = BitmapFactory.decodeFile(image_string);
        imageview.setImageBitmap(bm);
        }

    private File getTempFile(Context context)
        {
        File path = new File(Environment.getExternalStorageDirectory(), context.getPackageName() );
        if(!path.exists())
            path.mkdir();
        return new File(path, "debris.jpg");
        }

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
    if (resultCode == RESULT_OK)
        {
        switch(requestCode)
            {
            case 1:
            final File file = getTempFile(this);
            try
                {
                Media.getBitmap(getContentResolver(), Uri.fromFile(file) );
                image_string = Uri.fromFile(file).toString();
                Bitmap bm = Bitmap.createScaledBitmap(
                    BitmapFactory.decodeFile(image_string),
                    getResources().getDisplayMetrics().widthPixels,
                    getResources().getDisplayMetrics().heightPixels, 
                    true);
                imageview = (ImageView) findViewById(R.id.debris_view);
              imageview.setImageBitmap(bm);
                }
            catch (FileNotFoundException e)
                {
                Toast.makeText(getApplicationContext(), "file not found exception", Toast.LENGTH_SHORT).show();
                }
            catch (IOException e)
                {
                Toast.makeText(getApplicationContext(), "ioexception", Toast.LENGTH_SHORT).show();
                }
            break;
            }
        }
    }
}

also, here is my xml file:

<merge xmlns:android="http://schemas.android.com/apk/res/android">
    <ImageView  
        android:id="@+id/debris_view"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:scaleType="centerCrop"
        android:src="@drawable/icon"
        />
    <Button
        android:id="@+id/camera"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginBottom="20dip"
        android:layout_gravity="center_horizontal|bottom"
        android:padding="12dip"
        android:background="#AA000000"
        android:textColor="#ffffffff"
        android:text="would you like to take a new debris picture?"
        />
</merge>

Upvotes: 2

Views: 6525

Answers (3)

CodingBarfield
CodingBarfield

Reputation: 3398

You're loading in an image that is probably just too big. Downscale the image you want to show on screen to a smaller size and it should be fine. The logcat should show something helpfull about it.

Upvotes: 0

Sergey Glotov
Sergey Glotov

Reputation: 20356

You need move these 2 lines into onActivityResult():

case 1:
    final File file = getTempFile(this);
    try {
        Media.getBitmap(getContentResolver(), Uri.fromFile(file) );
        image_string = Uri.fromFile(file).toString();
        Bitmap bm = BitmapFactory.decodeFile(image_string);
        imageview.setImageBitmap(bm);
    } catch (FileNotFoundException e) {
        ...

Code in takePhoto() will be executed immediately after startActivityForResult() (i.e. it will not wait finishing called activity). Processing result must be done in onActivityResult().

Upvotes: 3

Femi
Femi

Reputation: 64700

Move

Bitmap bm = BitmapFactory.decodeFile(image_string);
        imageview.setImageBitmap(bm);

from takePhoto to

 try
                        {
                        Media.getBitmap(getContentResolver(), Uri.fromFile(file) );
                        image_string = Uri.fromFile(file).toString();
Bitmap bm = BitmapFactory.decodeFile(image_string);
        imageview.setImageBitmap(bm);
                        }
                    catch (FileNotFoundException e)

Upvotes: 1

Related Questions