How to share images that are not stored on device? - android

I have a application where i convert the text coming from user into a QR code and display the QR code in imageView. My problem is how can i use sharing Intent on this image as their no path specified for it nor the image is stored anywhere ?

You can do it by saving the bitmap of image in cache followed by sharing it.
//To get the bitmap from Imageview
imageView.setDrawingCacheEnabled(true);
Bitmap bitmap = imageView.getDrawingCache();
//To share it via Intent**
try {
File file = new File(getContext().getCacheDir(), fileName + ".png");
FileOutputStream fOut = new FileOutputStream(file);
bitmap.compress(CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
file.setReadable(true, false);
final Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
intent.setType("image/png");
startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}

Related

How to implement an Image Slider that becomes full screen when clicked?

How can I add an image slider that becomes full screen when clicked?
I would like to be able to preview the images in full screen mode and be able to share it or zoom it. What are some ways that I can implement this?
Use this lib to zoom image and for sharing an image you can use share intent
Bitmap icon = mBitmap;
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
icon.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
File f = new File(Environment.getExternalStorageDirectory() + File.separator + "temporary_file.jpg");
try {
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/temporary_file.jpg"));
startActivity(Intent.createChooser(share, "Share Image"));

Share Image from url

I am tring to share an image which i fetch from Json. After a number of attempts i finally concluded with this code. It works well till i open whatsapp and select the contact i want to share, the image doesnt load from there and when i click on submit, it says sharing failed.
Drawable drawable1 = imageViewPreview.getDrawable();
Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.id.image_preview);
File file = new File(SlideshowDialogFragment.this.getCacheDir(), String.valueOf(largeIcon + ".png"));
// FileOutputStream fOut = new FileOutputStream(file);
// largeIcon.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
file.setReadable(true, false);
final Intent intent = new Intent(Intent.ACTION_SEND);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
intent.setType("image/png");
startActivity(intent);
if i remove the comments
FileOutputStream fOut = new FileOutputStream(file);
will show error message and i have to add try/catch then, the app crashes saying Nullpoint exception
Try this code:
Bitmap bitmap = mBitmap;
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
File f = new File(Environment.getExternalStorageDirectory() + File.separator + "photo.jpg");
try {
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/photo.jpg"));
startActivity(Intent.createChooser(share, "Share Image"));

How to properly dispose of image bitmap after emailing a screenshot?

New to android. I'm taking a screen shot of my activity, and emailing it out. Here's my process:
public void emailScreenShot() {
//get bitmap of activity
View v = DeSlip.this.getWindow().getDecorView();
Bitmap bitmap = Bitmap.createBitmap(v.getWidth(),
v.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
v.draw(canvas);
//save bitmap externally
String path = Environment.getExternalStorageDirectory().toString();
File file = new File(path, AName + "vs" + BName+ ".JPEG");
Uri pngUri = Uri.fromFile(file);
FileOutputStream fos;
//compress
try {
fos = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
Log.e("BoutSlip", e.getMessage(), e);
} catch (IOException e) {
Log.e("BoutSlip", e.getMessage(), e);
}
//email intent
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("image/png");
emailIntent.putExtra(android.content.Intent.EXTRA_STREAM, pngUri);
startActivity(Intent.createChooser(emailIntent, "Send image using:"));
}
This works well, and gets my stuff out into the internet. My question is regarding using the external storage (I have permission in manifest). Is there some process I should be doing in order to remove or delete this bitmap after I have finished using it before I close? Or will the android memory management automatically take care of that with other stack items?
My gut says no, however you do want the bitmap around long enough to email (but I wasn't seeing it on the gallery app, so I know it's not available there).

Not able to share an Image on Whatsap

I am writing an app to share image.
I wrote the code but getting problems.
What I am doing
1: Saving the image temporarily in Internal Stoarge
2: Passing the URI to share the image.
Image is successfully saved in internal storage but not getting share on whatsapp.
When I share it on whatsapp, Whatsapp get opens, I select recipient, whatsapp processes it and says "Retry".
Code:
public void shareImage(View V)
{
Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawable.download);
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
icon.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
File file = new File(Environment.getExternalStorageDirectory()
+ File.separator + "myDownloadedImage.jpg");
try
{
file.createNewFile();
FileOutputStream fo = new FileOutputStream(file);
fo.write(bytes.toByteArray());
fo.close();
} catch (IOException e) {
Toast.makeText(this, "Some error in Writing"+e.getMessage(), Toast.LENGTH_LONG).show();
e.printStackTrace();
}
Uri downloadLocation=Uri.fromFile(file);
share.putExtra(Intent.EXTRA_STREAM, downloadLocation);
startActivity(Intent.createChooser(share, "Share Image"));
}
Image succesfully get saved on internal storage but not getting shared on whatsapp.
The screenshot is below. We can see images are not shared successfully.
Use below code to fetch image and make uri:
File file = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS), "share_image_" + System.currentTimeMillis() + ".png");
file.getParentFile().mkdirs();
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
out.close();
bmpUri = Uri.fromFile(file);
Now pass the bmpUri in:
shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
This should make your image share.

Saving image file and returning to whatsapp

I've used the following code to save an image file to the external storage
drawView.setDrawingCacheEnabled(true);
String img = MediaStore.Images.Media.insertImage(getContentResolver(),drawView.getDrawingCache(),UUID.randomUUID().toString()+".jpeg", "doodle");
I want to return this saved file to Whatsapp using an intent. Can somebody please help me out with the code? I'm new to android and would really appreciate it! Thanks :)
Try this code
Bitmap icon = mBitmap;
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
icon.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
File f = new File(Environment.getExternalStorageDirectory() + File.separator + "temporary_file.jpg");
try {
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/temporary_file.jpg"));
startActivity(Intent.createChooser(share, "Share Image"));

Categories

Resources