简体   繁体   中英

Trying to display image taken from camera intent

I'm Currently trying to take a photo from the default camera intent from the MainActivity and then put that image in an ImageView in the same activity.

I'm saving the images such that the image taken overwrites the previously taken image (In this case, I call the image test.jpg and I store it in sdcard)

The problem I have with my code right now is the ImageView displays the photo taken the previous time the application ran.

Here is my code.

public class MainActivity extends Activity 
{
ImageView imv;
@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    imv = (ImageView)findViewById(R.id.imv);
    Uri uri = null;
    String path = Environment.getExternalStorageDirectory().getAbsolutePath()+"/test.jpg";
    try
    {
        uri = takePhoto(path);
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    imv.setImageURI(uri);
}

private Uri takePhoto(String path) throws IOException
{
    File photo = new File(path);
    photo.createNewFile();
    Uri fileUri = Uri.fromFile(photo);
    Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
    startActivityForResult(cameraIntent, 0);
    fileUri = Uri.fromFile(new File(path));
    return fileUri;
}   
}

Try to set image in onActivityResult as below:

  protected void onActivityResult(int requestCode, int resultCode, Intent ata)                                     
          {
if (requestCode == 0) {
    if (resultCode == RESULT_OK) {

       imv = (ImageView)findViewById(R.id.imv);
        Bitmap photo = (Bitmap) data.getExtras().get("data");
        imv.setImageBitmap(photo);
    }}}
 private static final int     PICK_CONTACT_REQUEST = 0 ;
 protected void onActivityResult(int requestCode, int resultCode,
                 Intent data) {
             if (requestCode == PICK_CONTACT_REQUEST) {
                 if (resultCode == RESULT_OK) {

                    imv.setImageURI(uri);

                 }
             }
         }

Set the image in onactivity result it will show new Image taken from the camera .

Try this,

private Uri takePhoto(String path) throws IOException
    {
        File photo = new File(path);
        photo.createNewFile();
        if(photo.exists())
            photo.delete();
        photo = new File(path);
        Uri fileUri = Uri.fromFile(photo);
        Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
        startActivityForResult(cameraIntent, 0);
        fileUri = Uri.fromFile(new File(path));
        return fileUri;
    }   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM