How Do I Save An Imageview As An Image?
Solution 1:
When saving and loading, you need to get the root path of the system, first. This is how I'd do it.
Fileroot= Environment.getExternalStorageDirectory();
FilecachePath=newFile(root.getAbsolutePath() + "/DCIM/Camera/image.jpg");
Solution 2:
I've come across a couple solutions which are not solving this problem.
Here is a solution that worked for me. One gotcha is you need to store the images in a shared or non app private location (http://developer.android.com/guide/topics/data/data-storage.html#InternalCache)
Many suggestions say to store in the Apps
"private" cache location but this of course is not accessable via other external applications, including the generic Share File intent which is being utilised. When you try this, it will run but for example dropbox will tell you the file is no longer available.
/* STEP 1 - Save bitmap file locally using file save function below. */
localAbsoluteFilePath = saveImageLocally(bitmapImage);
/* STEP 2 - Share the non private Absolute file path to the share file intent */
if (localAbsoluteFilePath!=null && localAbsoluteFilePath!="") {
IntentshareIntent=newIntent(Intent.ACTION_SEND);
UriphototUri= Uri.parse(localAbsoluteFilePath);
Filefile=newFile(phototUri.getPath());
Log.d("file path: " +file.getPath(), TAG);
if(file.exists()) {
// file create success
} else {
// file create fail
}
shareIntent.setData(phototUri);
shareIntent.setType("image/png");
shareIntent.putExtra(Intent.EXTRA_STREAM, phototUri);
activity.startActivityForResult(Intent.createChooser(shareIntent, "Share Via"), Navigator.REQUEST_SHARE_ACTION);
}
/* SAVE IMAGE FUNCTION */
private String saveImageLocally(Bitmap _bitmap) {
File outputDir = Utils.getAlbumStorageDir(Environment.DIRECTORY_DOWNLOADS);
File outputFile = null;
try {
outputFile = File.createTempFile("tmp", ".png", outputDir);
} catch (IOException e1) {
// handle exception
}
try {
FileOutputStream out = new FileOutputStream(outputFile);
_bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
out.close();
} catch (Exception e) {
// handle exception
}
return outputFile.getAbsolutePath();
}
/* STEP 3 - Handle Share File Intent result. Need to remote temporary file etc. */
@OverridepublicvoidonActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// deal with this with whatever constant you use. i have a navigator object to handle my navigation so it also holds all mys constants for intentsif (requestCode== Navigator.REQUEST_SHARE_ACTION) {
// delete temp fileFilefile=newFile (localAbsoluteFilePath);
file.delete();
Toastertoast=newToaster(activity);
toast.popBurntToast("Successfully shared");
}
}
/* UTILS */
publicclassUtils {
//...publicstaticFilegetAlbumStorageDir(String albumName) {
// Get the directory for the user's public pictures directory.File file =
newFile(Environment.getExternalStorageDirectory(), albumName);
if (!file.mkdirs()) {
Log.e(TAG, "Directory not created");
}
return file;
}
//...
}
I hope that helps someone.
Post a Comment for "How Do I Save An Imageview As An Image?"