当前位置: 动力学知识库 > 问答 > 编程问答 >

android - Unable to load image in a ImageView from the Internal Storage

问题描述:

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.

网友答案:

You need this permission:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

READ_EXTERNAL_STORAGE not WRITE_EXTERNAL_STORAGE

网友答案:

@CommonsWare has already answered this. Try changing how you get your path. (Original Answer)

Why would this happen?

Because the path to external storage has changed over the years, which is why you should have never hard-coded the path in the first place.

how can I get the path directory in a more "formal and right" way instead of hardcoding the path?

Use Environment.getExternalStorageDirectory().

On your test environment, /sdcard is a symlink or hardlink to /storage/sdcard0, as set up by the device manufacturer. However, there is no guarantee that all devices will have such an /sdcard symlink.


If that does not help you. This definitely will: Reading from internal storage.

UPDATE

Please look for all usages of this "/download/img" ...

You are missing a "/" ... Replace it with "/download/img/"

分享给朋友:
您可能感兴趣的文章:
随机阅读: