Java – Android – When working with images, is it better to store them or use them in temporary memory?

Android – When working with images, is it better to store them or use them in temporary memory?… here is a solution to the problem.

Android – When working with images, is it better to store them or use them in temporary memory?

I’m trying to build an app that takes a picture with the camera and sends it back to the main activity for display in ImageVew. See a tutorial, save the photos to the SD card after taking them. I was able to save the file, but I had trouble getting the location where the image was stored.

I think it’s too much work to store an image in an SD card because the image isn’t that important.

Is there a way to “save” the image I just took with my camera into a bitmap element? If so, is it more efficient than storing in an SD card?


This 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()));
    }
}}

Solution

It is best to only save on external/internal storage before accessing it. It is best to use a third-party library to handle all image rendering and its lazy loading, for example:

Most of these libraries will focus on processing resources from HTTP URLs, but they can still be used to load information from local files or content providers.

Manipulating images with bitmaps can easily lead to OutOfMemoryErrors, such as shown at this link. However, In this case, since you are only using 1 bitmap, you don’t need to worry too much.

Related Problems and Solutions