编程设置ImageView资源不起作用



我正在尝试实现一个功能,用户可以从存储中选择图像文件,并且该图像在缩略图大小的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

我尝试在涉及毕加索(理想解决方案(的许多堆栈溢出帖子上找到许多不同的解决方案,从各种方法创建位图,设置imageView uri等...

没有设置ImageView资源的程序化方法,即使在我的Drawables中的PNG上也可以。我只能在XML中成功设置android:src

理智检查:

  • 图像视图是可见的,正确约束,并具有适当的高度和宽度。
  • 我可以在XML中设置一个源图像,并出现。我还设置了背景颜色,以确保它在屏幕上。XML一切都很好。
  • 我也可以成功地编程设置ImageView背景颜色,因此我知道我在代码中引用了正确的ImageView。

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"/> 

分配图像视图:

//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);
        }
    }

这里的任何其他想法或见解都是非常感谢。我肯定会在这堵墙上殴打头。

谢谢。

我发现了问题。

由于我的图像视图驻留在片段中,而不是直接在活动中,所以我在 onResume()被调用之前调用 onImageUrlReceived(Uri uri) 。在片段恢复之前,onActivityResult被调用。结果,映像视图的设置正确了,但是随后一秒钟,当调用onResume时,我正在重新分配imageView,因此看起来好像什么也没做。

结果,我现在将URI存储为片段成员,在onResume()中:
attachThumbnail = getActivity().findViewById(R.id.thumbnail);
if (userAttachmentUri != null){
    Picasso.with(getContext()).load(userAttachmentUri).fit().into(attachThumbnail);
}```

相关内容

最新更新