I am trying to add the Image from the the Internal Memory.
I am trying to use this code for that
ImageView message_image=(ImageView)v.findViewById(R.id.message_image);
File imgFile = new File(img_db);
Log.e("img_db",img_db+"------------->");
if(imgFile.exists()){
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
message_image.setImageBitmap(myBitmap);
}
here img_db is my path that i am getting from my Sqlite table.when i log it i get the fath like this
E/img_db: file:///storage/sdcard0/download/img11024FILE.jpg------------->
I have already given this permission in my manifest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
For downloading image and save to the phone i am using this code
public String downloadFile(String url_i_) {
try {
URL url
= new URL(url_i_);
Log.e("url_fetch--->img", String.valueOf(url));
HttpURLConnection urlConnection = (HttpURLConnection) url
.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
// connect
urlConnection.connect();
// set the path where we want to save the file
SDCardRoot = Environment.getExternalStorageDirectory();
// create a new file, to save the downloaded file
rand = new External_function().getnRandom();
file = new File(SDCardRoot, "/download/img" + rand+".jpg");
FileOutputStream fileOutput = new FileOutputStream(file);
InputStream inputStream = urlConnection.getInputStream();
// this is the total size of the file which we are downloading
totalSize = urlConnection.getContentLength();
// create a buffer...
byte[] buffer = new byte[1024];
int bufferLength = 0;
while ((bufferLength = inputStream.read(buffer)) > 0) {
fileOutput.write(buffer, 0, bufferLength);
downloadedSize += bufferLength;
// update the progressbar //
float per = ((float) downloadedSize / totalSize) * 100;
/*cur_val.setText("Downloading.... " + downloadedSize
+ "KB / " + totalSize + "KB (" + (int) per
+ "%)");*/
String i = "Downloading.... " + downloadedSize
+ "KB / " + totalSize + "KB (" + (int) per
+ "%)";
}
fileOutput.close();
file = new File(Environment.getExternalStorageDirectory() + "/download/img" + rand+".jpg"); // set your audio path
file_path= String.valueOf(Uri.fromFile(file));
} catch (final Exception e) {
Log.e("////////////////file", String.valueOf(e));
}
return file_path;
}
And inside my service class when i get the image path in return then i store it inside my sqlite table like this
String img = socketOperator.downloadFile(event_img);
localstoragehandler.insert(id, msg_user_by, msg_read_by, msg_content, msg_grp_id, msg_sent_time,type,event_time,event_lat,event_log,event_name,event_address,img);
I checked the path with image and id are same.I try this but really don't know what I am doing wrong.