So I have this weird Issue. 9/10 times it will work perfectly fine, but that 10% will not work at all.
I am trying to simply have an ImageView that I can zoom and pan. that works great- then I try to save the imageView to the phone itself. This is where MOST of the time it will work, but then sometimes it will save the image as a black screen...and not what's actually displayed on the ImageView.
Here is my code for saving the ImageView:
protected void saveZoomedImage(){
//create ImageView Bitmap
RelativeLayout tableContent;
tableContent=(RelativeLayout)findViewById(R.id.imgHolder);
tableContent.measure(MeasureSpec.makeMeasureSpec(tableContent.getLayoutParams().width, MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(tableContent.getLayoutParams().height,MeasureSpec.EXACTLY));
tableContent.setDrawingCacheEnabled(true);
final Bitmap screenshot = Bitmap.createBitmap(tableContent.getDrawingCache());
tableContent.setDrawingCacheEnabled(true);
//save file
File folder = new File(Environment.getExternalStorageDirectory () + "/thedir");
if(!folder.exists()){
folder.mkdir();
}
String exsistingFileName=Environment.getExternalStorageDirectory().getAbsolutePath() + "/thedir";
FileOutputStream fos = null;
try {
fos = new FileOutputStream(exsistingFileName + "/image.jpg");
screenshot.compress(Bitmap.CompressFormat.JPEG, 90, fos);
fos.flush();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
it saves the file fine, but it just seems like it doesn't capture the image for some reason on a small %age of times.
First make sure that your app has the storage permission enabled:
Go to Device Settings>Device>Applications>Application Manager>"your app">Permissions>Enable Storage permission!
Permissions in manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
So, if you want to create your own directory in your File Storage you can use somethibng like:
String myDate = getCurrentDateAndTime();
FileOutputStream outStream = null;
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File(sdCard.getAbsolutePath() + "/YourFolderName");
dir.mkdirs();
String fileName = "YourFileName_"+myDate+".jpg";
File outFile = new File(dir, fileName);
outStream = new FileOutputStream(outFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
outStream.flush();
outStream.close();
//to refresh the gallery
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(Uri.fromFile(file));
sendBroadcast(intent);
}
Helper function:
private String getCurrentDateAndTime() {
Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
String formattedDate = df.format(c.getTime());
return formattedDate;
Hope this helps!
Try this code:
public static String saveImageInExternalCacheDir(Context context, Bitmap bitmap, String myfileName) {
String fileName = myfileName.replace(' ', '_') + getCurrentDate().toString().replace(' ', '_').replace(":", "_");
String filePath = (context.getExternalCacheDir()).toString() + "/" + fileName + ".jpg";
try {
FileOutputStream fos = new FileOutputStream(new File(filePath));
bitmap.compress(Bitmap.CompressFormat.JPEG, 85, fos);
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
return filePath;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.