我想在Nexus 7平板电脑上使用Android默认图像查看器打开内部文件夹中的图像。 我使用以下代码,但由于某种原因,图像不会显示。我做错了什么?该文件的路径是:
file:///data/data/com.example.denandroidapp/files/Attachments/photoTemp/photo.jpg
(这是Uri.parse(“file://”+ file)返回的内容。)
ArticlePhoto photo = new ArticlePhoto(soapObject);
File f = new File(context.getFilesDir() + "/Attachments/photoTemp");
if(!f.exists())
f.mkdirs();
if (photo.ArtPhoto != null) {
Bitmap articlePhoto = BitmapFactory.decodeByteArray(photo.ArtPhoto, 0, photo.ArtPhoto.length);
ByteArrayOutputStream bytesFile = new ByteArrayOutputStream();
articlePhoto.compress(Bitmap.CompressFormat.JPEG, 100, bytesFile);
File file = new File(f + "/photo.jpeg");
try {
if(!file.exists())
file.createNewFile();
FileOutputStream outStream = new FileOutputStream(file);
outStream.write(bytesFile.toByteArray());
outStream.close();
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("file://" + file),"image/jpeg");
startActivity(intent);
} catch(Exception ex) {
AlertDialog alert = new AlertDialog.Builder(context).create();
alert.setTitle("Warning!");
alert.setMessage(ex.getMessage());
alert.show();
}
}
答案 0 :(得分:5)
试试这个:
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
Uri uri = Uri.parse("file://" + file.getAbsolutePath());
intent.setDataAndType(uri,"image/*");
startActivity(intent);
感谢。
答案 1 :(得分:2)
问题是图像是应用程序内部的!因此,外部应用程序(图像查看器)无法访问应用程序内部的数据。
您可能需要做的是创建内容提供商。 http://web.archive.org/web/20111020204554/http://www.marcofaion.it/?p=7
Android Manifest.xml
<provider android:authorities="com.example.denandroidapp" android:enabled="true" android:exported="true" android:name=<fully classified name of provider class>>
</provider>
创建意图
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri uri = Uri.parse("content://com.example.denandroidapp/" + filename);
intent.setDataAndType(uri, "image/jpeg");
答案 2 :(得分:2)
如果文件与您的应用程序关联(存储在应用程序空间的内部存储中),则其他应用程序无法直接访问您的文件,只要提供有效的文件路径即可。相反,您必须创建一个文件提供程序并生成一个内容uri。
首先,在您的AndroidManifest.xml中添加文件提供程序
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.mydomain.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
然后,您需要在xml / file_paths.xml中创建一个名为file_paths的文件(默认情况下未创建目录xml,因此请创建它)。
file_paths.xml看起来像
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="myFiles" path="./"/>
</paths>
添加尽可能多的路径,以使提供商可以访问。
最后您需要创建意图
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
File imagePath = new File(context.getFilesDir(), "fileName");
Uri contentUri = FileProvider.getUriForFile(context, "com.mydomain.fileprovider", imagePath);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setDataAndType(contentUri,"image/*");
context.startActivity(intent);
注意:确保在file_paths.xml和新File(context.getFilesDir(),“ fileName”)中分隔的文件路径匹配。 getFilesDir()将为您提供应用程序的根目录。
答案 3 :(得分:1)
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(outputFileName)),"image/jpeg");
startActivity(intent);
答案 4 :(得分:0)
选中此项:https://stackoverflow.com/a/11088980/1038442;
File file = new File(filePath);
MimeTypeMap map = MimeTypeMap.getSingleton();
String ext = MimeTypeMap.getFileExtensionFromUrl(file.getName());
String type = map.getMimeTypeFromExtension(ext);
if (type == null)
type = "*/*";
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri data = Uri.fromFile(file);
intent.setDataAndType(data, type);
startActivity(intent);
PS:如果您正在尝试打开.jpg文件,请尝试 替换字符串ext = MimeTypeMap.getFileExtensionFromUrl(file.getName()); 使用字符串\ text = MimeTypeMap.getFileExtensionFromUrl(&#34; .jpg&#34;);
祝你好运。答案 5 :(得分:0)
您可以使用扩展ContentProvider的FileProvider
检查链接-
https://developer.android.com/reference/android/support/v4/content/FileProvider
要指定FileProvider组件本身,请在您的应用清单中添加一个“ provider”元素。
ABC1
您必须为包含要获取内容URI的文件的每个目录指定“路径”的子元素。例如,这些XML元素指定两个目录
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.mydomain.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
此后,您需要为文件生成内容URI,然后调用该意图,请参考下面的链接
https://developer.android.com/reference/android/support/v4/content/FileProvider#GetUri
答案 6 :(得分:0)
就我而言,图库启动了,但未显示任何图像,而是直接转到主页。我的情况与OP所面对的情况大不相同,但我认为在这里值得一提,因为问题在于图像不是通过隐式意图显示的。
我的问题来自下面的代码。
val intent = context.packageManager.getLaunchIntentForPackage(packageName ?: "")
上面的代码告诉PackageManager
启动应用程序的入口点,而不是启动显示图像的活动。
如果您查看上面的Logcat,可以发现用cat=[android.intent.category.Launcher]
启动的意图将转到SplashActivity
。发生这种情况是因为我使用getLaunchIntentForPackage()
替代方法是将Intent
与setPackage()
一起使用,如下面的代码
val intent = Intent()
val uri = Uri.fromFile(file) // You should probably replace with ContentProvider's uri
intent.apply {
flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
action = Intent.ACTION_VIEW
setPackage(packageName)
setDataAndType(uri, "image/*")
}
context.startActivity(intent)