使用ACTION_VIEW从android中的缓存文件夹打开图像



这是我的代码

imgBtn.setOnClickListener(新的OnClickListener{

        @Override
        public void onClick(View v) {
            Cursor curso1 = dbUtil.getImgBlabreport(strLabReqId, strCat,
                    strTestCode);
            if (curso1.moveToFirst()) {
                curso1.moveToFirst();
                img = curso1.getBlob(curso1
                        .getColumnIndex(DbHelper.PHOTO_FIELD));

                ByteArrayInputStream imageStream = new ByteArrayInputStream(
                        img);
                bitmap = BitmapFactory.decodeStream(imageStream);
                File f1 = context.getCacheDir();
                String url = "data/data/com.rajinfotech.patientinfo/cache/";
                try {
                    File dir = new File(url);
                    if (!dir.exists()) {
                        dir.mkdirs();
                    }
                    OutputStream fOut = null;
                    File file = new File(url, "reportimg1.png");

                    if (file.exists())
                        file.delete();
                    file.createNewFile();
                    fOut = new FileOutputStream(file);
                    bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
                    fOut.flush();
                    fOut.close();
                    ImageView();

                } catch (Exception e) {
                    Log.e("saveToExternalStorage()", e.getMessage());
                }
                curso1.close();
            }

            else {
                Toast.makeText(context, "No image to visible",
                        Toast.LENGTH_SHORT).show();
            }
        }
    });

public void ImageView(){

    String path = "data/data/com.rajinfotech.patientinfo/cache/reportimg1.png";
    // Uri uri = Uri.parse(path);
    File targetFile = new File(path);
    Uri uri = Uri.fromFile(targetFile);
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.addCategory(Intent.CATEGORY_DEFAULT);
    intent.setDataAndType(uri, "image/*");
    startActivity(intent);

}

运行此代码时,我的图库以黑屏打开,但图像在缓存文件夹中,有人能帮我吗。。

您可以使用文件提供程序

在manifest.xml 中

<provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities="be.myapplication"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/file_paths" />
</provider>

在res/xml 中创建file_paths.xml

<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <cache-path name="my_cache" path="." />
</paths>

使用

File file = new File(getCacheDir(), "test.png");
Uri uri = FileProvider.getUriForFile(context, "be.myapplication", file);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setDataAndType(uri, "image/*");
startActivity(intent);

因为您的程序没有读取的权限

/数据/数据/。。。。

像一样改变路径

/SD卡/患者信息/缓存/

将解决此问题。

最新更新