How can i save the image taken from camera intent

2019-09-19 08:10发布

In my app the user can take an image from the camera and use as a profile picture. Everything works, except that when the user leaves the app and returns then the image isn't there anymore. So how can I save that image to stay there even when the user exits the app?

Code for camera intent:

Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
            startActivityForResult(cameraIntent, CAMERA_REQUEST); 

And onActivityResult:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
    if (requestCode == CAMERA_REQUEST) {  
        Bitmap photo = (Bitmap) data.getExtras().get("data"); 
        getLayoutInflater().inflate(R.layout.custon_header,null);
        ImageView profimg = (ImageView) findViewById(R.id.roundedimg);
        profimg.setImageBitmap(photo);

Could someone please assist me with this?

Thanks

3条回答
狗以群分
2楼-- · 2019-09-19 08:18

You can configure the intent to store the image, using EXTRA_OUTPUT like this:

 Intent imageIntent = new Intent(
            MediaStore.ACTION_IMAGE_CAPTURE);
    File imageFolder = new File(Environment
            .getExternalStorageDirectory(), "YourFolderName");
     if (!imagesFolder.exists()) {
        imagesFolder.mkdirs();
    }
    File imageFile = new File(imagesFolder, "user.jpg");
    Uri uriSavedImage = Uri.fromFile(image);
    imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
    imageIntent.putExtra(MediaStore.EXTRA_SCREEN_ORIENTATION,
            ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    activity.startActivityForResult(imageIntent, CAMERA_REQUEST);

So you have the path and can decode the file to bitmap.

查看更多
虎瘦雄心在
3楼-- · 2019-09-19 08:33

You need to save that image in sd-card or phone memory so that it can be accessed when you open your app again. You are seeing that image as soon as you take the image because it is there in the memory and as soon as you leave the app, the memory is released.

private void SaveBitmapInDirectory(Bitmap bitmap, String fileName) 
{
   File direct = new File(Environment.getExternalStorageDirectory() + "/dirName");

   if (!direct.exists()) 
   {
       File profilePic = new File("/sdcard/dirName/");
       profilePic.mkdirs();
   }

   File file = new File(new File("/sdcard/dirName/"), fileName);
   if (file.exists()) 
   {
      file.delete();
   }

   try 
   {
        FileOutputStream out = new FileOutputStream(file);
       bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
       out.flush();
       out.close();
   } 
   catch (Exception e) 
   {
      e.printStackTrace();
   }
}

And load the file from directory in onCreate function by calling the following function:

void loadProfilerImage(String fileName)
{
    File file = new File(new File("/sdcard/dirName/"), fileName);
    if (file.exists()) 
    {
        Bitmap myBitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
        ImageView.setImageBitmap(myBitmap);
    }
}
查看更多
何必那么认真
4楼-- · 2019-09-19 08:40
data.getExtras().get("data"); 

only returns the thumbnail of the image which would be on low quality. You have to specify on your camera intent a location where the image will be saved:

File outputFile = new File(Environment.getExternalStorageDirectory() +"/myOutput.jpg");

Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
    cameraIntent.putExtra(Intent.EXTRA_OUTPUT,outputFile.getAbsolutePath());

startActivityForResult(cameraIntent, CAMERA_REQUEST);

and on your activityResult, simply use the outputfile for your bitmap:

bitmap = BitmapFactory.decodeFile(outputFile.getAbsolutePath());
查看更多
登录 后发表回答