Java 使用文件路径在库中打开图像

Java 使用文件路径在库中打开图像,java,android,eclipse,Java,Android,Eclipse,我的数据库中存储了一个图像文件路径。现在使用它,我想在我的图库中打开SD卡上的图像。我该怎么做呢。我已经看到了Uri中的方法并使用了这个方法,但我发现了一个错误 File file = new File(filename); Uri uri = Uri.fromFile(file); Intent intent = new Intent(Intent.ACTION_VIEW, uri); intent.setType("im

我的数据库中存储了一个图像文件路径。现在使用它,我想在我的图库中打开SD卡上的图像。我该怎么做呢。我已经看到了Uri中的方法并使用了这个方法,但我发现了一个错误

File file = new File(filename);
            Uri uri = Uri.fromFile(file);
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            intent.setType("image/*");
            startActivity(intent); /** replace with your own uri */
如何修复它

04-20 08:42:23.516: E/AndroidRuntime(16815): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=file:///mnt/sdcard/My%20App/image_umxto_1.jpg }

致以最诚挚的问候你可以使用下面的一行来完成你的任务:

startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("content://media/external/images/media/16"))); /** replace with your own uri */

您还可以使用以下代码:

Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("file://" + "/sdcard/test.jpg"), "image/*");
startActivity(intent);

使用此代码并告诉我

您使用的uri必须是lke“file://sdcard/....“@agarwal我已添加错误我的文件路径不是uristringyou可以将文件路径转换为uri…uri.fromFile(新文件(文件路径))uri.fromfile在较新的android版本中提供FileUriExposed异常i get thts error
android.os.FileUriExposedException://filepath在此处//通过Intent.getData()在应用程序之外公开
使用您提供的代码时会出现问题中给出的相同错误
Intent intent = new Intent();
            intent.setAction(Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.fromFile(new File(filename)), "image/*");
            startActivity(intent);