【问题标题】:Image uploaded from gallery doesn't show in ImageView从图库上传的图像不会显示在 ImageView 中
【发布时间】:2017-02-22 13:38:45
【问题描述】:

我知道关于这个主题还有其他几个问题,但没有一个解决方案对我有用。我在清单中添加了权限。我可以打开图库选择一张照片并返回到应用程序,但 imageView 没有改变。该应用程序正在进行一些处理,我没有收到任何错误。我也尝试过缩放图像,然后再将其插入我的图像视图,但没有成功。

这是我的代码:

public class UserDetailsFragment extends Fragment {
private Context context;
private ImageView imageView;
private Bitmap uploadedImage;
public static final int RESULT_IMAGE = 0;


public UserDetailsFragment() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    context =  getActivity();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_user_details, container, false);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState){
    imageView = (ImageView) getView().findViewById(R.id.profile_picture);
    imageView.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(i, RESULT_IMAGE);
        }
    });
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(resultCode == RESULT_OK){
        Uri selectedImage = data.getData();
        String[] filePathColumn = {MediaStore.Images.Media.DATA};
        Cursor cursor = context.getContentResolver().query(
                selectedImage, filePathColumn, null, null, null);
        cursor.moveToFirst();
        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String filePath = cursor.getString(columnIndex);
        cursor.close();
        try {
            InputStream in = new URL(filePath).openStream();
            uploadedImage = BitmapFactory.decodeStream(in);
            imageView.setImageBitmap(uploadedImage);
        }catch (Exception ex){

        }
    }
}

}

这是我的图像视图:

<ImageView
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_marginTop="-160dp"
    android:layout_gravity="center"
    android:layout_below="@+id/imageView"
    android:src="@drawable/user_details_icon"
    android:id="@+id/profile_picture" />

编辑

我尝试改用毕加索,但仍然没有运气:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(resultCode == RESULT_OK){


        Picasso.with(context)
                .load(MediaStore.Images.Media.DATA)
                .resize(10, 10)
                .centerCrop()
                .into(imageView);

    }

【问题讨论】:

  • 首先,永远不要在没有记录的情况下捕获Exception,因为您是onActivityResult()。其次,您的onActivityResult() 代码是错误的,因为您可能无权使用文件路径(例如,它指向可移动存储)并且您正在主应用程序线程上执行磁盘 I/O。使用image-loading library,如Picasso,将Uri 传递给它,以正确使用Uri 并异步填充ImageView
  • 感谢您的提示。我对Android相当陌生,所以我实际上并不了解毕加索。我不确定我是否理解您对 onActivityResult() 的意思,但我尝试将其更改为内部存储,但仍然没有运气。我也改变了我的整个 onActivityResult(见编辑)但仍然没有运气

标签: android xml imageview


【解决方案1】:

在您之前的代码中,问题出在以下几行:

InputStream in = new URL(filePath).openStream();
uploadedImage = BitmapFactory.decodeStream(in);

这些行正在创建异常,因为它负责从服务器下载图像并将其转换为位图。当您从本地存储中选择图像时,图像的路径类似于以下

/storage/emulated/0/DCIM/Camera/IMG_20170209_183830841.jpg

当你将它作为参数传入 new URL() 时,它会创建以下异常

java.net.MalformedURLException:找不到协议

在您的情况下,首先您需要在清单文件和 onActivityResult() 中添加 READ_EXTERNAL_STORAGE 权限,您必须替换

InputStream in = new URL(filePath).openStream();
uploadedImage = BitmapFactory.decodeStream(in);

 File image = new File(filePath);
 BitmapFactory.Options bmOptions = new BitmapFactory.Options();
 Bitmap bitmap = BitmapFactory.decodeFile(image.getAbsolutePath(), bmOptions);
 bitmap = Bitmap.createScaledBitmap(bitmap, imageView.getWidth(), imageView.getHeight(), true);

注意:如果是棉花糖,别忘了在你的代码中添加安全权限。

【讨论】:

    【解决方案2】:

    我尝试改用毕加索,但仍然没有运气:

    那是因为您没有使用正确的Uri。您正在使用指向整个扫描媒体集合的Uri

    如果你阅读the documentation for ACTION_PICK,它有:

    输出:所选项目的 URI。

    这里,有问题的Uri是通过在传递给onActivityResult()Intent上调用getData()获得的。

    所以,将.load(MediaStore.Images.Media.DATA) 替换为.load(data.getData())

    此外,您很可能需要增加图像的大小,因为 10 像素 x 10 像素的图像会相当小。

    【讨论】:

    • 现在我得到 E/MiniThumbFile: Unable to create .thumbnails directory /storage/emulated/0/DCIM/.thumbnails
    • @Andypandy:确保您拥有WRITE_EXTERNAL_STORAGE 作为权限,包括在您的targetSdkVersion 为23 或更高时处理运行时权限。
    • 我在清单中添加了读写外部存储的权限。我的目标 SDK 是 19
    • @Andypandy:您是在硬件还是模拟器上进行测试?如果您在模拟器上进行测试,您是否将其配置为具有一些外部存储?否则,我不知道为什么 MediaStore 会难以创建该目录。
    • 我正在硬件上测试:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-16
    • 1970-01-01
    • 2020-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多