【发布时间】:2016-01-26 02:55:37
【问题描述】:
我正在尝试从内部存储器添加图像。
我正在尝试使用此代码
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);
}
这里的 img_db 是我从我的 Sqlite 表中获取的路径。当我记录它时,我得到了这样的路径
E/img_db: file:///storage/sdcard0/download/img11024FILE.jpg------------->
我已经在我的清单中授予了这个权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
为了下载图像并保存到我正在使用此代码的手机
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;
}
在我的服务类中,当我得到图像路径作为回报时,我将它存储在我的 sqlite 表中,就像这样
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);
我检查了带有图像和 id 的路径是否相同。我尝试了这个,但真的不知道我做错了什么。
【问题讨论】:
-
您还没有告诉我们到底是什么问题。你这么牛逼?
ImageView中没有任何内容吗?如果没有这些信息,就很难为您提供帮助。 -
@Xaver 感谢您的回复,实际上我的图像没有加载到图像视图中
-
但乍一看:您说您的清单中已经拥有
WRITE_EXTERNAL_STORAGE权限。但是你也有READ_EXTERNAL_STORAGE权限吗?你知道...既然你试图阅读而不是写作? -
@XaverKapeller 我添加了 READ_EXTERNAL_STORAGE 权限,但问题没有解决
标签: android android-sqlite android-imageview android-bitmap internal-storage