Sharing a png image in drawable folder

前端 未结 5 1726
野趣味
野趣味 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

    
    

    Add this line to android manifest for the write permission.

提交回复
热议问题