Reputation: 1
This is my code
imgBtn.setOnClickListener(new 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);
}
While running this code my gallery is opening with a black screen,but the image is in the cache folder,can any one help me on this..
Upvotes: 0
Views: 2011
Reputation: 500
Because your program don't have read access to
/data/data/....
Change path like
/sdcard/patientinfo/cache/
will fix the issue.
Upvotes: 0
Reputation: 1560
You can use File provider
In 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>
create file_paths.xml in res/xml
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<cache-path name="my_cache" path="." />
</paths>
Use
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);
Upvotes: 1