Reputation: 212
Im new to android and java programming, please explain in detail what should i do from here. I want to download an image and store it in a SharedPreferences
.
any variable will do, as long as you can also show me how to output the image as imageview
.
i read similar topics but i cant understand fully and i don't know how to work with those.
here's my code, class extends AsyncTask -> onPreExecute:
protected void onPostExecute(String result) {
SharedPreferences pref = getApplicationContext().getSharedPreferences(
"athan", MODE_PRIVATE);
Editor editor = pref.edit();
try{
JSONArray jArray = new JSONArray(result);
for(int i=0;i<jArray.length();i++)
{
JSONObject json_data = jArray.getJSONObject(i);
editor.putString("fstime"+i, json_data.getString("time"));
editor.putString("fspname"+i, json_data.getString("postname"));
//this is what i want to modify:
//editor.putString("fsfile"+i, json_data.getString("file"));
//"json_data.getString("file")" gives a value of string of link to the image
//so instead of saving the link, i want to download the image and store in in SharedPreference instead.
//like editor.putImage("fsfile"+i, json_data.getImage("file"));
}
editor.putBoolean("fileshares", true);
editor.commit();
}
catch(JSONException e)
{
Log.e("Athan", "Error parsing data "+e.toString());
}
fileshareview();
}
}
EDIT: So i done some research and found out that:
In order to do this, i have to: 1.convert the string given from my json_data into a URL 2. use this URL to get the image into Bitmap 3. convert Bitmap into String using Base64 encode 4. store the encoded string into the shared preferences.
then to show the image i have to: 1. revert back the encoded image back into Bitmap 2. use Bitmap to view as ImageView
my question is, i've been using the codes scattered accross here but still no luck, can someone help me understand how to do this? my phone always gets Logcat message "Grow Heap". i know this is not a good plan but in order to create "Offline access", i need the shared preferences.
Upvotes: 0
Views: 603
Reputation: 212
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(mylink), "image/*");
startActivity(intent);
this code works like charm..
Upvotes: 0
Reputation: 2289
Download the image bitmap:
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(imgUrl); // url as string
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
Bitmap result = streamToBitmap(is);
is.close();
Create directory to store image:
private void createFile() {
File file = new File(getDir());
file.mkdirs(); // Create directory
try {
File file1 = new File(getFileDir());
file1.createNewFile(); // Create file in the same directory
} catch (IOException e) {
e.printStackTrace();
}
}
// Get directory path. The directory is created in variable location
private String getDir() {
return Environment.getExternalStorageDirectory() + File.separator
+ DIRNAME;
}
// Get file path. The path is inside the previously created directory
private String getFileDir() {
return Environment.getExternalStorageDirectory() + File.separator
+ DIRNAME + File.separator + FILENAME; // "myPic.bmp"
}
Save image:
FileOutputStream fos = new FileOutputStream(getFileDir());
ByteArrayOutputStream outstream = new ByteArrayOutputStream();
myBitmap.compress(Bitmap.CompressFormat.JPEG, 85, outstream);
byte[] byteArray = outstream.toByteArray();
stream.write(byteArray);
stream.close();
The main thread shouldn't download the image file, instead create a Thread
object or use android AsyncTask
.
After the image is stored on the devices internal memory, all you need is to save the file location by calling getFileDir()
method and saving the result in shared memory
Upvotes: 1