Shareable PNG image in a selectable folder

I integrate the share with the following code for the application.

private void socialShare() { Uri uri = Uri.parse("android.resource://com.example.myproject/drawable/appicon"); Intent shareIntent = new Intent(); shareIntent.setAction(Intent.ACTION_SEND); shareIntent.putExtra(Intent.EXTRA_STREAM, uri); shareIntent.putExtra(Intent.EXTRA_TEXT, "sharing myapp"); shareIntent.setType("image/jpeg"); startActivity(Intent.createChooser(shareIntent, "Share from")); } 

As in the above code, I am trying to put a png image , which is in a folder with the ability to transfer. But the image could not be sent. Is it because in setType is the image / jpeg ? I can not use jpeg because it loses transparency. Can someone please invite me to share them with the image?

Here is the code that I use to copy the image from drawable to sdcard:

 String commonPath = Environment.getExternalStorageDirectory().toString() + "/MyAppFolder"; File direct = new File(commonPath); if(!direct.exists()) { if(direct.mkdir()) { Log.d("tag","directory created"); //directory is created; } } Bitmap bm = BitmapFactory.decodeResource( getResources(), R.drawable.sharingimage); OutputStream outStream = null; File savingFile = new File(commonPath, "shareImage.png"); if(!savingFile.exists()) { Log.d("tag","file is created"); try { savingFile.createNewFile(); outStream = new FileOutputStream(savingFile); bm.compress(Bitmap.CompressFormat.PNG, 100, outStream); outStream.flush(); outStream.close(); Log.d("tag","Saved"); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } 
+7
android share social-networking
source share
5 answers

I found a solution where I do not need to copy the image on the SD card. Here he is:

  try { Uri imageUri = null; try { imageUri = Uri.parse(MediaStore.Images.Media.insertImage(this.getContentResolver(), BitmapFactory.decodeResource(getResources(), R.drawable.fragment_menu_logo), null, null)); } catch (NullPointerException e) { } String text = getString(R.string.share_text); // Launch the Google+ share dialog with attribution to your app. Intent shareIntent = new PlusShare.Builder(mActivity) .setType("image/*") .setText(text) .addStream(imageUri) .getIntent(); startActivity(shareIntent); } catch (android.content.ActivityNotFoundException ex) { Toast.makeText(mActivity, mActivity.getString(R.string.share_google_plus_not_installed), Toast.LENGTH_LONG).show(); } 
+12
source share

You can use the following method ...

 Intent shareIntent = new Intent(); shareIntent.setAction(Intent.ACTION_SEND); shareIntent.setType("image/png"); Uri uri = Uri.parse("android.resource://your package name/"+R.drawable.ic_launcher); shareIntent.putExtra(Intent.EXTRA_STREAM, uri); shareIntent.putExtra(Intent.EXTRA_TEXT, "Hello, This is test Sharing"); startActivity(Intent.createChooser(shareIntent, "Send your image")); 

Tried and tested ... worked for me

+10
source share
 Bitmap bitmap= BitmapFactory.decodeResource(getResources(),R.drawable.xxxx); String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)+"/LatestShare.jpg"; OutputStream out = null; File file=new File(path); try { out = new FileOutputStream(file); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out); out.flush(); out.close(); } catch (Exception e) { e.printStackTrace(); } path=file.getPath(); Uri bmpUri = Uri.parse("file://"+path); Intent shareIntent = new Intent(); shareIntent = new Intent(android.content.Intent.ACTION_SEND); shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri); shareIntent.setType("image/jpg"); startActivity(Intent.createChooser(shareIntent,"Share with")); 

This works fine for me, and it will require write permission

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 

Add this line to the android manifest to allow writing.

+7
source share

Add permission first

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

use bitmap from res

 Bitmap b =BitmapFactory.decodeResource(getResources(),R.drawable.userimage); Intent share = new Intent(Intent.ACTION_SEND); share.setType("image/jpeg"); ByteArrayOutputStream bytes = new ByteArrayOutputStream(); b.compress(Bitmap.CompressFormat.JPEG, 100, bytes); String path = MediaStore.Images.Media.insertImage(getContentResolver(), b, "Title", null); Uri imageUri = Uri.parse(path); share.putExtra(Intent.EXTRA_STREAM, imageUri); startActivity(Intent.createChooser(share, "Select")); 

Tested via bluetooth.

Works like a spell.

+3
source share

You cannot directly share uri with your internal application storage (of course, the resources of your application will always be in the internal storage)

There are two ways to achieve this.

  • Copy your image to external storage, then open it. Cm.

  • Write a content provider to share images. To do this, contact Create and share the file from the internal storage

+1
source share

All Articles