4

I'm trying to build an app that takes a picture with the camera an sends it back to the main activity to display it in an ImageVew. I saw a tutorial that saves the image in the SD Card when the picture is taken. I'm able to save the file but I'm having difficulties getting the location of the stored image.

I think that storing the image in the SD Card is too much work since that image is not that important.

Is there a way to "save" the image I just took with the camera in a BitMap element? if so is it more efficient than storing it in the SD Card?


Here is my MainActivity.java:

public class MainActivity extends AppCompatActivity {
private Uri imgLocation;
ImageView mImageView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button capture = (Button) findViewById(R.id.am_btn_camera);
    capture.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

            imgLocation = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "fname_" +
                    String.valueOf(System.currentTimeMillis()) + ".jpg"));

            intent.putExtra(MediaStore.EXTRA_OUTPUT, imgLocation);
            startActivityForResult(intent, 1);
        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == 1 && resultCode == RESULT_OK) {

        Log.e("URI",imgLocation.toString());
        mImageView.setImageBitmap(BitmapFactory.decodeFile(imgLocation.toString()));
    }
}}
Etienne GT
  • 185
  • 8
  • You could use an in-memory solution such as storing the bitmap in a singleton. But keep in mind this will eat your Android memory. – Tim Biegeleisen Apr 04 '16 at 00:58
  • Thanks Tim. I went for the approach of storing it in a bitmap but it is a smaller version of the image, it'll do for now. Regards. – Etienne GT Apr 04 '16 at 01:09

3 Answers3

3

It is better to just save it on the external/internal storage and then access it. It is even better to use a third-party library to handle all the image rendering and their lazy-loading like:

Most of these libraries will focus on handling resources from HTTP URLs, but they can still be used to load information from a local file or a Content Provider.

Handling images with Bitmaps might cause OutOfMemoryError easily as shown in this link. Although, in this case, since you are only using 1 Bitmap, there is not a lot to worry about.

Community
  • 1
  • 1
Evin1_
  • 12,292
  • 9
  • 45
  • 47
  • 1
    For an example. Load image from filesystem using picasso http://stackoverflow.com/questions/23681177/picasso-load-image-from-filesystem – mubeen Apr 14 '16 at 04:52
1

It's almost always better to save it in the file system and pass the filepath within activities.

Android Bitmaps literally consume a lot of memory, and it's never a good idea to keep it in memory unless it's really required.

I know, it's a bit of a hassle to store it in the file system and pass the file reference, but it'll prevent a lot of unwanted outOfMemory errors later within your app.

Ruchira Randana
  • 4,021
  • 1
  • 27
  • 24
0

Use Picasso. It will automatically cache the file for you on the filesystem.

If you need to retain that file on disk for longer periods, you must save it yourself.

xyz
  • 3,349
  • 1
  • 23
  • 29