ImageView Share Intent - android

Hi I have a gallery in my app which shows the image in an image view when you click on the thumbnail. I am setting up a Long Click Listener for an alert dialog that has 2 buttons one to set as wallpaper and one to share. I have the share intent and the get drawing cache somewhat working. It works in the emulator but not on my phone. I have used many of the examples on this site to get this going but its not working at all. it keeps force closing the app on my phone. any help is appreciated.
alertDialog.setButton2("Share",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
imgView.setDrawingCacheEnabled(true);
Bitmap b = imgView.getDrawingCache();
File sdCard = Environment.getExternalStorageDirectory();
File file = new File(sdCard, "image.jpg");
FileOutputStream fos;
try {
fos = new FileOutputStream(file);
b.compress(CompressFormat.JPEG, 95,fos);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.d("Save Example", "Image saved.");
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/jpeg");
intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/image.jpg"));
startActivity(Intent.createChooser(intent, "Share Picture Via.."));
}
});
alertDialog.show();
return true;
Update: it seems to be a problem with
b.compress(CompressFormat.JPEG, 95, fos);
it keeps saying null pointer.
Turns out the null pointer is actually a read_only error..So its not actually writing the file i get a ioexception read-only file system. Why is it doing this?

imgView.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); imgView.layout(0, 0, imgView.getMeasuredWidth(), imgView.getMeasuredHeight());
See if this helps before
Bitmap b = imgView.getDrawingCache();
ALso see if this helps
Android drawing cache

Related

How can I share an image that I have taken in my app?

I currently have an app in which I'm taking photos and I'm trying to share them directly through the app. The problem I'm having is that when the chooser comes up and I pick one, I get an error toast that pops up and says "Unable to attach file". Then it just takes me back into the app and everything else still works. I'm assuming that this means that I have to save the image first, but I'm not sure if I'm doing that correctly. I found some examples online of how to save the images but I'm not sure if that's the problem or if the share intent is the problem.
final Button shareButton = (Button) findViewById(R.id.shareButton);
shareButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
imageView.setDrawingCacheEnabled(true);
Bitmap b = imageView.getDrawingCache();
MediaStore.Images.Media.insertImage(getContentResolver(), b, "title", "description");
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/jpeg");
BitmapDrawable bitmapDrawable = (BitmapDrawable) imageView.getDrawable();
Bitmap image = bitmapDrawable.getBitmap();
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
File f = new File(Environment.getExternalStorageDirectory() + File.separator + "temp.jpg");
try {
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(Environment.getExternalStorageDirectory().getPath()));
startActivity(Intent.createChooser(shareIntent, "Share Image"));
}
});
I have "title" and "description" in as placeholders until I can get this to work.
Try to replace :
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(Environment.getExternalStorageDirectory().getPath(‌​)));
With :
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(f));
This question can help you.

Android view image intent not displaying image

I'm trying to tap an image view, and open that image inside the default photo viewer:
void handleOpenImage(){
try {
File temp = File.createTempFile("myImage", ".png");
BitmapDrawable bitmapDrawable = (BitmapDrawable) attachedImageView.getDrawable();
Bitmap bitmap = bitmapDrawable.getBitmap();
FileOutputStream stream = new FileOutputStream(temp);
boolean success = bitmap.compress(Bitmap.CompressFormat.PNG, 0, stream);
if(!success){
throw new Exception();
}
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(temp), "image/*");
startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}
}
However, when I call this function, a gallery activity starts, but doesn't display my image. The image file is created successfully at the temp path, double checked that. Why isn't the intent working?
As I asked in the comments, the absolute path of the temp file was /data/data/MY_APPS_IDENTIFIER/cache/ulouder-1004534880.png.
This path ist in the private space each app has and cannot be accessed by other apps for security reasons.
By saving the temp file to another location, the gallery app can access it and displays the image correctly.

How can I save my bitmap correctly for second time?

I want to save my bitmap to cache directory.
I use this code:
try {
File file_d = new File(dir+"screenshot.jpg");
#SuppressWarnings("unused")
boolean deleted = file_d.delete();
} catch (Exception e) {
// TODO: handle exception
}
imagePath = new File(dir+"screenshot.jpg");
FileOutputStream fos;
try {
fos = new FileOutputStream(imagePath);
bitmap.compress(CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
Log.e("GREC", e.getMessage(), e);
} catch (IOException e) {
Log.e("GREC", e.getMessage(), e);
}
}
it s working fine. But if I want to save different img to same path, something goes wrong. I mean it is saved to same path but I see it old image, but when I click the image I can see the correct image which I saved second time.
Maybe its come from cache but I do not want to see old image because when I want to share that image with whatsapp old image seen , if i send the image it seems correct.
I want to share saved image on whatsapp like this code:
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(imagePath));
shareIntent.setType("image/jpeg");
startActivityForResult(Intent.createChooser(shareIntent, getResources().getText(R.string.title_share)),whtsapp_result);
How can I fix it?
thanks in advance.
Finally I solved my problem like this:
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, getImageUri(context,bitmap_));
shareIntent.setType("image/jpeg");
startActivityForResult(Intent.createChooser(shareIntent, getResources().getText(R.string.title_share)),whtsapp_result);
v
public Uri getImageUri(Context inContext, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = Images.Media.insertImage(inContext.getContentResolver(), inImage, "TitleC1", null);
return Uri.parse(path);
}
This is just a guess, but since you save a new image (with the old name, or another, that’s not relevant) you should fire a media scan to that path so that the media provider is updated with new content.
See here:
MediaScannerConnection.scanFile(context, new String[]{imagePath}, null, null);
Or even better, wait for the scan to be completed:
MediaScannerConnection.scanFile(context, new String[]{imagePath}, null, new OnScanCompletedListener() {
#Override
void onScanCompleted(String path, Uri uri) {
// Send your intent now.
}
});
In any case this should be called after the new file is saved. As I said, I have not tested and this is just a random guess.

Picture got deleted after send via Google hangout

I have written a simple application to view pictures. However, after sending the picture with the common intent:
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("URLSTRING"));
shareIntent.setType("image/jpeg");
mActivity.startActivity(Intent.createChooser(shareIntent, "SHARE"));
The picture can be successfully sent if I chose Google hangout. But it is deleted after that!
Tested with other file manager application (root explorer) and it is the same behavior!
However, sent picture with GooglePlusGallery application does not seem to have this problem.
What is the trick? How to avoid the picture to be deleted?
I had the same problem with Hangouts. It seems that it deletes the file even if it doesn't succeed to send it, thus always copy the file to a new temp file before calling the intent.
I had the same issue, I believe exposing a ContentProvider instead that an uri could solve.
Same problem here. My app didn't even have storage write permissions, so I'm assuming Hangouts actually is deleting the images.
So instead of sharing the file directly, i make a copy first.
Here's the full solution:
Bitmap bm = BitmapFactory.decodeFile(filePath); // original image
File myImageToShare = saveToSD(bm); // this will make and return a copy
if (myImageToShare != null) {
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(myImageToShare));
shareIntent.setType("image/jpeg");
context.startActivity(Intent.createChooser(shareIntent, "Share using"));
}
private File saveToSD(Bitmap outputImage){
File storagePath = new File(Environment.getExternalStorageDirectory() + yourTempSharePath);
storagePath.mkdirs();
File myImage = new File(storagePath, "shared.jpg");
if(!myImage.exists()){
try {
myImage.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
} else {
myImage.delete();
try {
myImage.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
FileOutputStream out = new FileOutputStream(myImage);
outputImage.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
return myImage;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

Faster way to open cached bitmaps in the default gallery

I have written a bit of code that when the listview is clicked, the image at that location is stored to external memory, then the file path string is sent with the intent to view the image in the default gallery. The only problem is that it takes a seriously long amount of time (I'm talking 10+ seconds on my thunderbolt).
What I haved tried:
1. Storing the bitmap on internal memory
2. Lowering the quality of the bitmap
Here is the code:
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
if(position>0){
Bitmap bmp =adapter.getBitmap(adapter.getData(position-1));
if(bmp!=null){
//String path = context.getCacheDir().getAbsolutePath() + "/view.png";
//File f = new File(context.getCacheDir().getAbsolutePath(),"MemeCache");
//if(!f.exists())
// f.mkdirs();
String path = android.os.Environment.getExternalStorageDirectory().getAbsolutePath() + "/view.png";
Toast.makeText(context, "opening in gallery", Toast.LENGTH_SHORT).show();
File file = new File(path);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
bmp.compress(CompressFormat.PNG, 100, fos );
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(path)), "image/png");
//intent.setDataAndType(Uri.fromFile(f), "image/png");
activity.startActivity(intent);
}else{
Toast.makeText(context, arg0.getItemAtPosition(position).toString() +"is HaAAACkSS!!!!", Toast.LENGTH_SHORT).show();
}
}
}
});
Some things that might help:
1) add some timers or instrumentation to you code to see exactly where you're spending all the time: saving the bitmap to the sdcard, starting the intent, or something else entirely.
2) Once you've added timers and can measure the performance, you can see if shrinking the image or storing the image in a different place helps and if so how much. On some devices, internal storage is on the sdcard itself.
3) depending on your program, you might want to consider pre-saving the files to the sdcard (possibly in the background) so that they are probably already saved by the time the user tries to view them in the gallery.

Categories

Resources