【问题标题】:Error With sharing image分享图片时出错
【发布时间】:2016-01-29 11:40:23
【问题描述】:

我想使用 ACTION_SEND 分享一张图片。所以基本上当用户点击图像并选择“共享图像”时,它应该发送选择的图像, 因此,在测试时,它会将我带到我想与之共享的应用程序,即whatsapp、Facebook、电子邮件等。然后在选择其中一个时,它会显示“共享失败,请重试”。我似乎无法弄清楚为什么它不起作用。但是,我有相同的代码来使用 ACTION_VIEW 全屏显示图像文件,这似乎很好用,但不适用于共享。

public void Onmulti2 (View view) {

    Intent share = new Intent(Intent.ACTION_SEND);

    share.setType("image/*");

    String imagePath = Environment.getExternalStorageDirectory()
            + "/ahmed.jpg";

    File imageFileToShare = new File(imagePath);

    Uri uri = Uri.fromFile(imageFileToShare);
    share.putExtra(Intent.EXTRA_STREAM, uri);

    startActivity(Intent.createChooser(share, "Share Image!"));

}

【问题讨论】:

    标签: android android-intent android-image


    【解决方案1】:

    设置类型 image/jpg 而不是 image/*

    public void Onmulti2 (View view) {
    
    Intent share = new Intent(Intent.ACTION_SEND);
    
    share.setType("image/jpg");
    
    String imagePath = Environment.getExternalStorageDirectory()
            + "/ahmed.jpg";
    
    File imageFileToShare = new File(imagePath);
    
    Uri uri = Uri.fromFile(imageFileToShare);
    share.putExtra(Intent.EXTRA_STREAM, uri);
    
    startActivity(Intent.createChooser(share, "Share Image!"));
    

    }

    【讨论】:

    • 试试看,String imagePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/ahmed.jpg";
    • 你的图片在根目录吗?
    • 还是同样的错误...我的图片在drawable文件夹中
    • 比您以错误的方式获取图像路径此方法适用于从手机目录而不是从drawbles中获取的图像
    【解决方案2】:

    这对我有用

    首先将我的图像存储在外部存储中

    private void SaveImage(Bitmap finalBitmap) {
    
        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state)) {
    
            String root = Environment.getExternalStorageDirectory().toString();
            File myDir = new File(root + "/saved_images");
            myDir.mkdirs();
            String fname = "image.jpg";
            File file = new File(myDir, fname);
            if (file.exists()) file.delete();
            try {
                FileOutputStream out = new FileOutputStream(file);
                finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
                out.flush();
                out.close();
    
            } catch (Exception e) {
                e.printStackTrace();
            }
    
            MediaScannerConnection.scanFile(this, new String[]{file.toString()}, null,
                    new MediaScannerConnection.OnScanCompletedListener() {
                        public void onScanCompleted(String path, Uri uri) {
                            Log.e("ExternalStorage", "Scanned " + path + ":");
                            Log.e("ExternalStorage", "-> uri=" + uri);
                        }
                    });
        }
    }
    

    ## 。 ##

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        Bitmap img = BitmapFactory.decodeResource(getResources(), R.drawable.ahmed);
        SaveImage(img);
    
    }
    

    然后从外部存储加载我的图像然后共享它。

        public void Onmulti2 (View view) {
    
        Intent share = new Intent(Intent.ACTION_SEND);
        share.setType("image/jpeg");
        share.putExtra(Intent.EXTRA_STREAM, Uri.parse("/storage/emulated/0/saved_images/image.jpg"));
        startActivity(Intent.createChooser(share, "Share image using"));
    }
    

    注意

    从模拟器获取路径

    01-29 23:25:47.478 32648-32659/com.company.integrations E/ExternalStorage: Scanned /storage/emulated/0/saved_images/image.jpg:
    01-29 23:25:47.478 32648-32659/com.company.integrations E/ExternalStorage: -> uri=content://media/external/images/media/106290
    

    【讨论】:

      【解决方案3】:

      第一步:首先添加权限

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

      第 2 步:编辑

      Intent share = new Intent(Intent.ACTION_SEND);
      share.setType("image/jpeg");    
      sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(imagePath);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-01-16
        • 2014-04-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多