我正在尝试实现一项功能,用户可以从存储中选择一个图像文件,并且该图像将显示在缩略图大小的ImageView中。
文件选择部分似乎是正确的:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, GET_ATTACHMENT_RESULT);
//...
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == GET_ATTACHMENT_RESULT){
if (resultCode == RESULT_CANCELED){
// Anything TODO here?
} else if (resultCode == RESULT_OK){
Uri uri = data.getData();
addTimeFragment.onImageUrlReceived(uri);
}
}
}
返回的Uri似乎是内容Uri,其路径类似于:
content://com.android.providers.media.documents/document/image%3A14702
调用uri.getPath()返回:
/document/image:14702
我尝试了许多涉及Picasso的Stack Overflow帖子中找到的不同解决方案(理想解决方案),通过各种方法创建位图,设置ImageView Uri等...
即使在位于我的可绘制对象中的PNG上,也没有一种编程的方法来设置ImageView资源。我只能在XML中成功设置android:src
。
健全性检查:
XML:
<ImageView
android:id="@+id/thumbnail"
android:layout_width="48dp"
android:layout_height="48dp"
app:layout_constraintTop_toBottomOf="@id/time_entry_spacer"
app:layout_constraintStart_toStartOf="@id/camera_bg"
android:layout_marginBottom="4dp"
android:layout_marginStart="6dp"
android:background="#220000FF"/>
分配ImageView:
//attachThumbnail is my ImageView
attachThumbnail = getActivity().findViewById(R.id.thumbnail);
attachThumbnail.setBackgroundColor(context.getResources().getColor(R.color.colorAccent)); // This works as a sanity check
onImageUrlReceived方法的各种失败:
public void onImageUrlReceived(Uri uri){
Log.d(APP_TAG, "URI: " + uri.toString());
Log.d(APP_TAG, "URI: " + uri.getPath());
// Even loading a known drawable fails
// Picasso.with(context).load(R.drawable.calendar).fit().error(R.drawable.error).into(attachThumbnail);
// Picasso.with(context).load(uri).fit().error(R.drawable.error).into(attachThumbnail);
// Picasso.with(context).load(uri.getPath()).fit().error(R.drawable.error).into(attachThumbnail);
// File imgFile = new File(url);
// if(imgFile.exists()){
// Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
//
// attachThumbnail.setImageBitmap(myBitmap);
// }
// attachThumbnail.setImageURI(uri);
// Bitmap bitmap = null;
// try {
// bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), uri);
// } catch (IOException e) {
// e.printStackTrace();
// }
// attachThumbnail.setImageBitmap(bitmap);
// Bitmap b = BitmapFactory.decodeFile(uri.getPath());
// attachThumbnail.setImageBitmap(b);
// See DownloadImage AsyncTask below
// new DownloadImage(attachThumbnail).execute(uri.getPath());
// InputStream with scaling
// try {
// InputStream ims = getActivity().getContentResolver().openInputStream(uri);
// Bitmap b = BitmapFactory.decodeStream(ims);
// Bitmap b2 = Bitmap.createScaledBitmap(b, 50, 50, false);
// attachThumbnail.setImageBitmap(b2);
// } catch (FileNotFoundException e) {
// e.printStackTrace();
// }
// InputStream without Scaling
// Bitmap bitmap = null;
// try {
// bitmap = BitmapFactory.decodeStream(getActivity().getContentResolver().openInputStream(uri));
// } catch (FileNotFoundException e) {
// e.printStackTrace();
// }
// attachThumbnail.setImageBitmap(bitmap);
}
public class DownloadImage extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImage(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.d("Error", e.getStackTrace().toString());
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
非常 表示赞赏。我肯定是在这头墙上的墙上撞头。
谢谢。
答案 0 :(得分:0)
我发现了问题所在。
由于我的ImageView驻留在一个Fragment中,而不是直接位于Activity中,因此我在调用我的Fragment之前调用onImageUrlReceived(Uri uri)
onResume()
。在片段恢复之前调用onActivityResult
。结果,ImageView的设置正确,但是稍后,我在调用onResume时重新分配了ImageView,因此看起来好像什么也没做。
结果,我现在将Uri作为Fragment成员存储在onResume()
中:
attachThumbnail = getActivity().findViewById(R.id.thumbnail);
if (userAttachmentUri != null){
Picasso.with(getContext()).load(userAttachmentUri).fit().into(attachThumbnail);
}```