我正在尝试将 ImageView 设置为 uri.pare 的资源。
为了执行这个任务,我尝试了这个但它不起作用:无法加载图像
方法一:
Intent intent = new Intent(Intent.ACTION_ATTACH_DATA);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setDataAndType(Uri.parse("R.id.imageView1"),"image/*");
intent.putExtra("mimeType", "image/*");
startActivity(Intent.createChooser(intent, "Set as:"));
方法二:
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("R.id.imageView1"),"image/*");
startActivity(intent);
我无法将 ImageView 设置为 uri.parse 的资源,所以请帮助我如何将 ImageView 设置为 uri.parse 的资源
我找到了一段代码,但我不明白如何用我的代码调整这段代码:
这是我发现的:
ImageView imgView = (ImageView) findViewById(R.id.image3);
imgView.setImageURI(Uri.parse("file://mnt/sdcard/d2.jpg"));
我如何用我的代码调整它:
intent.setDataAndType(Uri.parse("R.id.imageView1"),"image/*");
最佳答案
ImageView.setImageURI 仅采用此处所述的 Android 内容 URI:
https://groups.google.com/forum/?fromgroups=#!topic/android-developers/jupslaeAEuo
换句话说,它采用“file://”或“content://”URI 之类的东西,而不是像您尝试做的那样的资源 ID。
如果您想这样做,我所知道的唯一方法以及我所做的就是将资源写入文件,然后将文件路径传递给 ImageView URI。关于如何向系统写入文件的例子有很多(包括考虑使用SD卡等)
我很确定您可以使用 ContentProvider openFile/openInputStream 方法编写一个 ContentProvider 来解析“content://”或“file://”方案,但我在需要它之前放弃了它。我可能会完成它 - 如果有人好奇它是如何工作的......
关于android - 将 uri.parse 资源设置为 ImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24982799/