Best way to save images which comes from server in

2019-02-18 13:24发布

I am working on one application and getting product images from the server. Currently I am saving those images into the SQLite database after converting them into byte array and at the time of use I convert them back into bitmap.

Now I have below two queries:

  1. What is the best way to save the images in app from where we can sync them also and get when ever required.

  2. How to assign them on image view or button because sometimes memory issue arises and application stop suddenly.

Please guide me. Any help is appreciated.

3条回答
看我几分像从前
2楼-- · 2019-02-18 13:30

Demo code

package com.example.demo;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;

public class MainActivity extends Activity {
    private AQuery androidQuery;

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

        ImageView img = (ImageView) findViewById(R.id.img);
        androidQuery = new AQuery(this);
        androidQuery.id(img).image(imgUrl, true, true);// donload and save to sdcard if it is already in sdcard will not download again.

    }

}
查看更多
The star\"
3楼-- · 2019-02-18 13:40

Storing app specific things on SD Card may not be the right way.. You should store images on

// it will reside in data/data/com.package.name 
String path = getApplicationContext().getFilesDir() + "/";
File imagesFolder = new File(path);
imagesFolder.mkDir();

and put image files here in that folder.. So SD Card dependency can be removed.

查看更多
来,给爷笑一个
4楼-- · 2019-02-18 13:41

Save them as a file on the external storage:

File downloadDir = new File(android.os.Environment.getExternalStorageDirectory().getAbsolutePath() + "/MyAppName/MyImages");
if(!downloadDir.exists()){
   downloadDir.mkdirs();
}

will create a directory to store the pictures in.

Then check out this guide here how to load only the right amount of pixels for your Image. Else you will get an OutOfMemory exception. (5MP picture will take up to 20MB of the heap)

查看更多
登录 后发表回答