1

(This question isn't duplicate !)

How can I share an image via intent ?

I tried this

Android Share Intent for a Bitmap - is it possible not to save it prior sharing?

How to share image to social media with bitmap?

share image with URL android share intent

but only worked on the emulator and did not work on the actual phone and made the following code error:

//b method : convert inputstream to bitmap
String bitmapPath = MediaStore.Images.Media.insertImage(getContentResolver(), b(inputStream) ,"title", null);

Full code :

InputStream is = getAssets().open(imageName.getText().toString());
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
String path = Environment.getExternalStorageDirectory()+"/" + APP_NAME()+"/pictures/"+ls+"/" ;
new File(path).mkdirs();
String fileName = System.currentTimeMillis() + imageName.getText().toString().replace("pic/" , "");
FileOutputStream fileOutputStream = new FileOutputStream(new File(path+fileName));
fileOutputStream.write(buffer);
fileOutputStream.close();
InputStream inputStream = new FileInputStream(new File(path+fileName));

String bitmapPath = MediaStore.Images.Media.insertImage(getContentResolver(), b(inputStream) ,"title", null);
Uri bitmapUri = Uri.parse(bitmapPath);

Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_STREAM , bitmapUri);
startActivity(intent);


//Intent intent = new Intent(android.content.Intent.ACTION_SEND);
//intent.putExtra(Intent.EXTRA_STREAM, bitmapUri);
//intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
//intent.setType("image/*");
//startActivity(intent);
RKRK
  • 1,284
  • 5
  • 14
  • 18
clasher
  • 97
  • 1
  • 11

1 Answers1

0

My problem was solved with a small change in my code:

Uri bitmapUri = Uri.parse (new File (path + fileName) .toString ());

Full code :

InputStream is = getAssets().open(imageName.getText().toString());
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
String path = Environment.getExternalStorageDirectory()+"/" + APP_NAME()+"/pictures/"+ls+"/" ;
new File(path).mkdirs();
String fileName = System.currentTimeMillis() + imageName.getText().toString().replace("pic/" , "");
FileOutputStream fileOutputStream = new FileOutputStream(new File(path+fileName));
fileOutputStream.write(buffer);
fileOutputStream.close();
//InputStream inputStream = new FileInputStream(new File(path+fileName));

//String bitmapPath = MediaStore.Images.Media.insertImage(getContentResolver(),b(inputStream) ,"title", null); // comment this line
Uri bitmapUri = Uri.parse(new File(path+fileName).toString()); //changed

Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_STREAM , bitmapUri);
startActivity(intent);

if (new File(path+fileName).exists()){
    new File(path+fileName).delete();
}
clasher
  • 97
  • 1
  • 11