我有一个向用户显示图片的应用。这听起来很简单,但我已经在这几天试图解决确切的问题。那么,用户将会发生什么:
java.io.FileNotFoundException: /storage/emulated/....jpg: open failed: ENOENT (No such file or directory)
。所以我认为我并没有真正通过正确的Uri,并试图从那里进行故障排除。然后我不小心旋转屏幕嘿 - 我的形象就在那里!当我将它旋转几次时,日志文件显示了这一点:
android.graphics.Bitmap@49361a20
android.graphics.Bitmap@495c42c8
android.graphics.Bitmap@496ac1e0
android.graphics.Bitmap@495b2b28
这是否显示内存位置?即使在屏幕旋转后,它应该是不同的吗?
有了这些信息,我还有其他地方可以解决这个问题吗?我可以排除Uri无效,因为它在旋转屏幕后显示正确的图像。
这是下载单击图像所涉及的代码(实用程序类); 此方法似乎运行良好,图像保存到正确的目录。
public void downloadFile(String url) {
File folder = new File(Environment.getExternalStorageDirectory() + directory);
String fileName = url.substring(url.lastIndexOf("/") + 1);
String filePath = folder.getPath() + "/" + fileName;
File file = new File(filePath);
fileUri = Uri.fromFile(file);
if (!folder.exists()) {
Log.i(TAG, "Worka folder is not found, creating one now");
folder.mkdir();
} else {
Log.i(TAG, "Worka folder is found");
if (file.exists()) {
Log.i(TAG, "Found a file with the same name, deleting/replacing it now");
file.delete();
}
}
DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
Uri downloadUri = Uri.parse(url);
DownloadManager.Request request = new DownloadManager.Request(downloadUri);
request.setAllowedNetworkTypes(
DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI)
//.setAllowedOverRoaming(false).setTitle("Roaming is not available")
.setDestinationInExternalPublicDir(directory, fileName);
downloadManager.enqueue(request);
Log.i(TAG, "downloadFile() is successful, " + fileName + " is saved to Worka directory");
}
public Uri getFilePath() {
return fileUri;
}
确保它从活动传递到另一个活动;
FileDownload fileDownload = new FileDownload(getApplicationContext());
fileDownload.downloadFile(conversation.getMessage());
Uri fileUri = fileDownload.getFilePath();
if (fileUri != null) {
Log.i(TAG, "fileUri: " + fileUri.toString());
Intent intent = new Intent(BulletinPostActivity.this, ImageViewingActivity.class);
intent.putExtra("imageUri", fileUri.toString());
startActivity(intent);
最后,从传递的Uri中获取Bitmap:
然后我尝试在这里使用AsyncTask,以确保位图正确加载......但我不确定要编写什么来实现此目的。
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
String imagePath = getIntent().getStringExtra("imageUri");
imageUri = Uri.parse(imagePath);
touchImageView = (TouchImageView) findViewById(R.id.image_viewing_image_view);
if (imageUri != null) {
new loadBitmap(imageUri).execute();
}
}
public class loadBitmap extends AsyncTask<Void, Integer, Bitmap> {
Uri uri;
public loadBitmap(Uri uri) {
this.uri = uri;
}
@Override
protected Bitmap doInBackground(Void... params) {
getBitmap();
return bitmap;
}
public Bitmap getBitmap() {
try {
Log.i(TAG, "Attempting to get the bitmap from imageUri");
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), imageUri);
Log.i(TAG, "Successfully gotten the bitmap from imageUri: " + bitmap);
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
if (bitmap == null) {
getBitmap();
} else {
touchImageView.setImageBitmap(bitmap);
}
}
}