【问题标题】:I am unable to share image using sharing intent and cannot create directory我无法使用共享意图共享图像并且无法创建目录
【发布时间】:2018-05-15 18:55:55
【问题描述】:

我无法使用共享意图传递图像。应用程序运行,但当我尝试在 WhatsApp 中将图像发送给朋友时,它显示“共享失败,请重试”并与gmail 它说“无法附加空文件”。我还在清单中设置了权限。我猜在创建文件或路径时存在一些问题。

请检查以下代码:

share.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {



            Bitmap icon = BitmapFactory.decodeResource(getResources(), img);
            Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
            sharingIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            sharingIntent.setType("image/*");
            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            icon.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
             File f = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)+ File.separator + "temporary_file.jpg");
           // File f1=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);

            //File f = new File(Environment.getExternalStorageDirectory() + "/save_picture", "temporary_file.jpg");

            try {
              f.createNewFile();
               // f.mkdir();
                f.createNewFile();
                FileOutputStream fo = new FileOutputStream(f);
                fo.write(bytes.toByteArray());
            } catch (IOException e) {
                e.printStackTrace();

            }
           // sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("content://" + Environment.getExternalStorageDirectory()));
             sharingIntent.putExtra(Intent.EXTRA_STREAM,Uri.parse("content:///sdcard/temporary_file.jpg"));
            startActivity(Intent.createChooser(sharingIntent, "Share via: "));


        }
    });

【问题讨论】:

  • logcat中有stacktrace吗?
  • 不,应用程序运行并且它不会崩溃。在 logcat 中没有看到异常或错误。
  • 您决定为位图的图像存储在哪里?在文件系统或内存中?
  • 嗯,好像是运行时权限问题
  • @MarcEstrada 实际上传入 Bitmap 的 img 是图像的整数值,甚至我可以看到图像从调试模式转换为位图。我猜它与文件有关。

标签: java android android-intent share


【解决方案1】:

我认为 URI 格式不正确。试试这个:

Uri.fromFile(file);

而不是

Uri.parse("content:///sdcard/temporary_file.jpg"); // Here probably file path is wrong

使用 Uri.fromFile 可以确保文件 URI 是正确的。

如果您的 targetSdkVersion 为 24+,您需要使用 content:// 而不是 file://。要迁移,请查看此问题。

android.os.FileUriExposedException: file:///storage/emulated/0/test.txt exposed beyond app through Intent.getData()

【讨论】:

  • 错误:android.os.FileUriExposedException: file:///storage/emulated/0/Pictures/temporary_file.jpg 通过 ClipData.Item.getUri() 暴露在应用之外
  • 好的,您的目标是 24 岁以上。看看我的答案,我刚刚用一个解决方案对其进行了编辑。
  • 但没有使用正确的方法。你的 Uri 路径错误
  • 我尝试了文件提供程序的方法,但在将图像分享到 WhatsApp 时仍然出现“不支持文件格式”的提示,您可以编辑我的代码 Uri 路径是否正确?
【解决方案2】:

对于 android 6 及更高版本,在 manifest 中添加权限是不够的,您必须在运行时请求权限。例如,对于外部存储权限,您必须这样做:

if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
    // permission granted, you can write external storage
}

如果没有被授予,你必须请求权限:

final int READ_STORAGE_PERMISSION_REQUEST_CODE = 1212;            
ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                WRITE_EXTERNAL_STORAGE);

}

要读取外部存储,您必须将WRITE_EXTERNAL_STORAGE 替换为READ_EXTERNAL_STORAGE

编辑:

在您的活动中覆盖 onRequestPermissionsResult :

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] results) {
    super.onRequestPermissionsResult(requestCode, permissions, results);
    if(PackageManager.PERMISSION_GRANTED == grantResults[0]){

        //The user accepted the permissions requests, you can do your stuffs here
    }
}

您可以找到更多信息here

【讨论】:

  • 测试设备的API等级是多少?
  • 出现此错误:E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.apple.wallpapershd, PID: 6274 java.lang.NumberFormatException: For input string: "android.permission.WRITE_EXTERNAL_STORAGE"在 java.lang.Integer.parseInt(Integer.java:608) 在 java.lang.Integer.parseInt(Integer.java:643) 在 com.example.apple.wallpapershd.WallpaperFullScreen$2.onClick(WallpaperFullScreen.java:94)
  • 测试设备为API 26
猜你喜欢
  • 1970-01-01
  • 2021-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-03
  • 1970-01-01
相关资源
最近更新 更多