【问题标题】:Share canvas image on android在 android 上分享画布图像
【发布时间】:2017-09-12 00:24:12
【问题描述】:

你好,所以我写了一个小游戏,最后你可以分享你的结果。结果使用画布写在图像上。问题是共享时出现错误“错误,找不到文件”。该错误仅在屏幕上显示,不会反映在 logcat 中。我已经花了无数个小时试图解决它,但似乎没有任何效果。我没有收到任何错误,但文件似乎仍然无法共享。有没有人建议它为什么不起作用?

快速回顾:加载位图,使其成为画布,绘制它,检查保存权限,保存它,获取已保存文件的 URI,使用共享意图中的 URI。我真的不明白缺少什么。

画布部分单独测试,我可以使用 fb 库将位图分享到 Facebook。不幸的是,android native share 不允许在不保存位图的情况下共享位图。

在清单中,我对内部和外部存储都有 WRITE 和 READ 权限。我真的很感激任何帮助。

点击监听器上的分享按钮:

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.Myimage); 
                mutableBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
                Canvas canvas = new Canvas(mutableBitmap);
                Paint paint = new Paint();
                paint.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                paint.setColor(Color.BLACK);
                paint.setTextSize(170);

                int top_margin = 1000;
                int left_margin = 1700;

                canvas.drawText("You got a ton of points", left_margin, top_margin, paint);

ActivityCompat.requestPermissions(test_process.this,
                            new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE},1);

许可结果:

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
        case 1: {
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                sharethis(mutableBitmap);
            } else {
                Toast.makeText(test_process.this, "Permission denied to read your External storage", Toast.LENGTH_SHORT).show();
            }
            return;
        }
    }
}

分享方式:

public void sharethis(Bitmap bitmap){

    File file_path = getFilesDir();

    File file = new File(file_path, "resultImg.jpg");
    FileOutputStream fOut;
    try {
        fOut = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fOut);
        fOut.flush();
        fOut.close();
    } catch (Exception e) {
        e.printStackTrace();
        Log.i("file saving problem", String.valueOf(e));
    }

    Uri uri = Uri.fromFile(file);
    Uri uriContent = getImageContentUri(this, file);

    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_SEND);
    intent.setType("image/jpeg");
    Log.i("Uri", String.valueOf(uri));
    Log.i("UriContent", String.valueOf(uriContent));
    intent.putExtra(Intent.EXTRA_STREAM, uriContent);
    startActivity(Intent.createChooser(intent, "Share Cover Image"));
}

和URI转换器:

public static Uri getImageContentUri(Context context, File imageFile) {
    String filePath = imageFile.getAbsolutePath();
    Cursor cursor = context.getContentResolver().query(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
            new String[] { MediaStore.Images.Media._ID },
            MediaStore.Images.Media.DATA + "=? ",
            new String[] { filePath }, null);

    if (cursor != null && cursor.moveToFirst()) {
        int id = cursor.getInt(cursor
                .getColumnIndex(MediaStore.MediaColumns._ID));
        Uri baseUri = Uri.parse("content://media/external/images/media");
        return Uri.withAppendedPath(baseUri, "" + id);
    } else {
        if (imageFile.exists()) {
            ContentValues values = new ContentValues();
            values.put(MediaStore.Images.Media.DATA, filePath);
            return context.getContentResolver().insert(
                    MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
        } else {
            return null;
        }
    }
}

【问题讨论】:

    标签: android canvas bitmap share


    【解决方案1】:

    getImageContentUri() 不起作用。你的文件在internal storage;第三方应用,包括MediaStore,无法访问它。

    摆脱getImageContentUri()Set up FileProvider 提供来自 getFilesDir() 的文件。然后,使用FileProvider.getUriForFile() 获得一个Uri,您可以在ACTION_SEND Intent 中使用它。

    还有:

    • 您需要将FLAG_GRANT_READ_URI_PERMISSION 添加到ACTION_SEND Intent

    • 你不需要 READ_EXTERNAL_STORAGE 来做这些

    【讨论】:

    • 您是否尝试设置ContentProvider 并覆盖openFile 方法而不是FileProvider(并将Bitmap 存储在物理文件中)?
    • 感谢您的解释。有用。如果有人会研究这个解决方案,我使用的文件路径如下: 。我花了一些时间才弄清楚。
    猜你喜欢
    • 1970-01-01
    • 2013-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多