In my app I download images from server using Volley and its ImageLoader (with BitmapLruCache). I want to create a share option, researched a little bit, and found that share intent can only share localy stored images. First question, is this right?
Second, if that is right, what should I do? Should I download image again and save it to local storage? Or put it in MediaStore? Or I can pull image from cache and share cached version? What are the best practices?
Any suggestion is welcome, or code snippet.
What I did is made another request for getting the image, and added an ImageListener which provides ImageContainer containing bitmap.
MyApplication.getImageLoader(activity).get(imageURL, new ImageListener() {
#Override
public void onErrorResponse(VolleyError error) {}
#Override
public void onResponse(ImageContainer response, boolean isImmediate) {
Bitmap mBitmap = response.getBitmap();
ContentValues image = new ContentValues();
image.put(Media.MIME_TYPE, "image/jpg");
Uri uri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, image);
try {
OutputStream out = getContentResolver().openOutputStream(uri);
boolean success = mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.close();
if (!success) {
} else {
imageUri = uri;
mShareActionProvider = (ShareActionProvider) item.getActionProvider();
// Create the share Intent
Intent shareIntent = ShareCompat.IntentBuilder.from(activity).setType("image/jpg").setText(shareText).getIntent();
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
mShareActionProvider.setShareIntent(shareIntent);
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
As image is already downloaded and cached (using BitmapLRU cache and ImageLoader), this new request gives me bitmap from cache so no new network data is made.
Related
I am uploading an image to my server and once uploaded my server responds with the new URI for it (can be the same URL as the old one), I want to remove the old cached image and insert the new one for the new URI.
I try to accomplish this by doing:
// Retrofit2 onResponse
String newImageUri = response.body().getUri();
String oldImageUri = Preferences.getUser().getImageUrl();
// Remove old image from cache
Fresco.getImagePipeline().evictFromCache(Uri.parse(oldImageUri));
Fresco.getImagePipeline().evictFromDiskCache(Uri.parse(oldImageUri));
Fresco.getImagePipeline().evictFromMemoryCache(Uri.parse(oldImageUri));
Fresco.getImagePipelineFactory().getMainFileCache().remove(new SimpleCacheKey(oldImageUri));
// Insert new image at new URI
try {
Fresco.getImagePipelineFactory().getMainFileCache().insert(new SimpleCacheKey(newImageUri), new WriterCallback() {
#Override
public void write(OutputStream os) throws IOException {
os.write(imageData); // byte[] or the new Bitmap
}
});
}
catch (Exception e) {
e.printStackTrace();
}
uriProfileImage.setImageURI(newImageUri);
There are no exceptions but I still only see the old image.
I solved it, it was an error with the Uri format for the new Uri...
Uri uri = Uri.parse("http://frescolib.org/static/fresco-logo.png");
Fresco.getImagePipelineFactory().getMainDiskStorageCache().remove(new CacheKey(uri.toString()));
Fresco.getImagePipelineFactory().getSmallImageDiskStorageCache().remove(new CacheKey(uri.toString()));
I'm trying to get fresco cached image from a fragment after a button click event to share the image bitmap in whatsapp!! Unfortunately it's very slow, after click the share button 2, 3 times code works!! I'm looking for better solution !!
ImageRequest imageRequest = ImageRequestBuilder
.newBuilderWithSource(Uri.parse("http://example.com/test,jpg"))// disck cached
.setRequestPriority(com.facebook.imagepipeline.common.Priority.HIGH)
.setLowestPermittedRequestLevel(ImageRequest.RequestLevel.DISK_CACHE)
.build();
// .setRequestPriority(Priority.HIGH)
DataSource<CloseableReference<CloseableImage>> dataSource =
imagePipeline.fetchDecodedImage(imageRequest, this);
try {
dataSource.subscribe(new BaseBitmapDataSubscriber() {
#Override
public void onNewResultImpl(#Nullable Bitmap bitmap) {
if (bitmap == null) {
Log.d(TAG, "Bitmap data source returned success, but bitmap null.");
return;
}
sharewithwhatsappBitmap(bitmap);
// The bitmap provided to this method is only guaranteed to be around
// for the lifespan of this method. The image pipeline frees the
// bitmap's memory after this method has completed.
//
// This is fine when passing the bitmap to a system process as
// Android automatically creates a copy.
//
// If you need to keep the bitmap around, look into using a
// BaseDataSubscriber instead of a BaseBitmapDataSubscriber.
}
#Override
public void onFailureImpl(DataSource dataSource) {
// No cleanup required here
Log.d(TAG, "Bitmap data source onFailureImpl");
}
}, CallerThreadExecutor.getInstance());
} finally {
if (dataSource != null) {
dataSource.close();
}
}
I'm using DraweeController in recyclerview, DraweeController is more optimized I guess !! but don't how to use it for getting one image with bitmap !!
public void sharewithwhatsappBitmap(Bitmap bitmap) {
String path = MediaStore.Images.Media.insertImage(getApplicationContext().getContentResolver(),
bitmap, "Image Description", null);
Uri bmpUri = Uri.parse(path);
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
//shareIntent.putExtra(Intent.EXTRA_SUBJECT, "Title Of The Post");
shareIntent.putExtra(Intent.EXTRA_TEXT, "https://play.google.com/store/apps/details?id=garbagebin.com.garbagebin");
shareIntent.setType("image/*");
//startActivity(Intent.createChooser(shareIntent, "Share Image"));
shareIntent.setPackage("com.whatsapp");
try {
startActivity(shareIntent);
} catch (android.content.ActivityNotFoundException ex) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=com.whatsapp")));
}
}
Log when first time it calls for bitmap:
v/unknown:AbstractDraweeController: controller 2a7f5b0e null -> 15: initialize
V/unknown:AbstractDraweeController: controller 2a7f5b0e 15: setHierarchy: com.facebook.drawee.generic.GenericDraweeHierarchy#20cc392f
My app can download an image from a raspberry. it works fine. This is the code
public void downloadFile() {
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect("******");
ftpClient.login("****","*****");
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
String remoteFile1;
File downloadFile1 = new File(filePath);
OutputStream outputStream1 = new BufferedOutputStream(new FileOutputStream(downloadFile1));
boolean success = ftpClient.retrieveFile(remoteFile1, outputStream1);
outputStream1.close();
if (success) {
System.out.println("File #1 has been downloaded successfully.");
} else {
System.out.println("Error in downloading file !");
}
boolean logout = ftpClient.logout();
if (logout) {
System.out.println("Connection close...");
}
} catch (IOException ex) {
System.out.println("Error: " + ex.getMessage());
ex.printStackTrace();
} finally {
try {
ftpClient.disconnect();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
And then I can display it so the user of my app can see it. For the image loading, Im using this code and it works too.
private void loadImage(String imagePath) {
Uri imageUri;
String fullImagePath;
Drawable image;
ImageView imageDisplay;
imageUri = Uri.parse(imagePath);
fullImagePath = imageUri.getPath();
image = Drawable.createFromPath(fullImagePath);
imageDisplay=(ImageView) findViewById(R.id.imageDisplay);
imageDisplay.setImageDrawable(image);
}
Now I want to display the image without downloading it in my gallery. But I can't figure out how to do this.
Can someone help me please.
You cannot show an image without download it. Actually when you see something "remotely", you are downloading it.
If you mean that the image is too large and you don't want to download, but want a mechanism for the user can view it. One possible solution is make a thumbnail (reduced image) in server side and show that "preview" to the user. Then if the user want to download it to the gallery you could get the original image.
If you want to display an image without downloading it, it has to be uploaded in a image hosting site or alike so you will just use the link instead of the whole FTP Client.
Basically, you are using a code that is intended for saving an image. And the one you are using for loading the images fetches data from the Drawable. So you are in the wrong path.
I have used com.nostra13.universalimageloader to load image. Inside the standard ImageAdapter in a ViewPager, there is a share button to share the bitmap. The code is as follows:
Code:
#Override
public Object instantiateItem(ViewGroup view, int position)
{
....
#Override
public void onLoadingComplete(String imageUri, View view, final Bitmap loadedImage)
{
spinner.setVisibility(View.GONE);
btn_share = (ImageButton) imageLayout.findViewById(R.id.btn_share);
btn_share.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
String file_path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Shining Card";
File dir = new File(file_path);
if (!dir.exists()) dir.mkdirs();
File file = new File(dir, "to share");
FileOutputStream fOut;
try
{
fOut = new FileOutputStream(file);
loadedImage.compress(Bitmap.CompressFormat.PNG, 85, fOut);
fOut.flush();
fOut.close();
}
catch (Exception e)
{
e.printStackTrace();
Constants.custom_toast(getActivity(), "Error when sharing", "");
}
Uri uri = Uri.fromFile(file);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("image/*");
intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "");
intent.putExtra(android.content.Intent.EXTRA_TEXT, "");
intent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(intent, "Share Cover Image"));
}
});
}
Question:
The problem lies on sharing the image.
Share via whatsapp
Sharing the first image via whatsapp, the image can be shared successfully to others. Continues with sharing second image it also works successfully.
However, if the sharing of the first image is cancelled in whatsapp and return to the app, and then choose another image to share in whatsapp, the preview in whatsapp will remain as the first image, though if ignore it and continue to share whatsapp will actually share the new image. (i.e. there is a preview problem) (using other app to share image perform the same, preview in whatsapp does not go wrong)
Share via gmail
Sharing the image as attachment via gmail, the image cannot be previewed nor opened.
I have an ImageView with a share intent( which works great, brings up all the apps I can share the image with), however, I can not share the photo because it has no path on my phone. How do I go about saving the ImageView on my phone? Below is my code.
public void taptoshare(View v)
{
View content = findViewById(R.id.myimage);
content.setDrawingCacheEnabled(true);
Bitmap bitmap = content.getDrawingCache();
File file = new File("/DCIM/Camera/image.jpg");
try
{
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(CompressFormat.JPEG, 100, ostream);
ostream.close();
}
catch (Exception e)
{
e.printStackTrace();
}
Intent shareIntent = new Intent(Intent.ACTION_SEND);
Uri phototUri = Uri.parse("/DCIM/Camera/image.jpg");
shareIntent.setData(phototUri);
shareIntent.setType("image/*");
shareIntent.putExtra(Intent.EXTRA_STREAM, phototUri);
startActivity(Intent.createChooser(shareIntent, "Share Via"));
}
}
UPDATE
Ok, so I figured it out. Now I have a new question, how would I go about saving this image to a new folder?
When saving and loading, you need to get the root path of the system, first. This is how I'd do it.
File root = Environment.getExternalStorageDirectory();
File cachePath = new File(root.getAbsolutePath() + "/DCIM/Camera/image.jpg");
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!="") {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
Uri phototUri = Uri.parse(localAbsoluteFilePath);
File file = new File(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. */
#Override
public void onActivityResult(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 intents
if (requestCode== Navigator.REQUEST_SHARE_ACTION) {
// delete temp file
File file = new File (localAbsoluteFilePath);
file.delete();
Toaster toast = new Toaster(activity);
toast.popBurntToast("Successfully shared");
}
}
/* UTILS */
public class Utils {
//...
public static File getAlbumStorageDir(String albumName) {
// Get the directory for the user's public pictures directory.
File file =
new File(Environment.getExternalStorageDirectory(), albumName);
if (!file.mkdirs()) {
Log.e(TAG, "Directory not created");
}
return file;
}
//...
}
I hope that helps someone.