Sharing a png image in drawable folder

前端 未结 5 1577
野趣味
野趣味 2020-11-27 19:02

I am integrating share with the following code for the app.

private void socialShare()
    {
        Uri uri = Uri.parse(\"android.resource://com.example.myp         


        
相关标签:
5条回答
  • 2020-11-27 19:05
    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 for me perfectly, and this will need write permission

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

    Add this line to android manifest for the write permission.

    0 讨论(0)
  • 2020-11-27 19:19

    You can share using 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...working for me

    0 讨论(0)
  • 2020-11-27 19:23

    You can not directly share a uri from you apps internal storage (of course the resourses of your app will be always in the internal storage)

    There are two ways of achieving this..

    1. Copy your image to external storage then share it from there. See this

    2. Write a Content Provider to share image. For that refer Create and Share a File from Internal Storage

    0 讨论(0)
  • 2020-11-27 19:25

    I found solution where I should not copy image on sd card. Here it 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();
        }
    
    0 讨论(0)
  • First add permission

    <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 Charm.

    0 讨论(0)
提交回复
热议问题