首页 > 解决方案 > 位图共享意图

问题描述

我正在尝试将整个 CardView 转换为图像,然后我想使用不同的应用程序共享它。

问题

cardView.setDrawingCacheEnabled(true);
        cardView.measure(View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED),
                            View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED));
        cardView.layout(0,0, cardView.getMeasuredWidth(), cardView.getMeasuredHeight());
        cardView.buildDrawingCache(true);
        bitmap = Bitmap.createBitmap(cardView.getDrawingCache());
        cardView.setDrawingCacheEnabled(false);
        String path = MediaStore.Images.Media.insertImage(DetailedActivity.this.getContentResolver(),bitmap,"quote",null);
        uri = Uri.parse(path);

事情是我将cardview转换为位图,但是从位图获取uri时它返回空路径。所以在转换 Uri 时它给出了 NULL POINTER EXCEPTION。

以下是我提供的权限列表。

<uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.MEDIA_CONTENT_CONTROL"
                        tools:ignore="ProtectedPermissions" />

CardView XML 如果我给 CardView 一个定义的 CardView 高度和宽度的硬编码值,它可以工作,但是当我使用

android:layout_width="match_parent"
android:layout_height="match_parent"

使用上面显示的代码,它给出了 NULL_POINTER_EXCEPTION。

查询

1.我怎样才能让它工作?

2.有没有其他方法可以做到这一点?

标签: androidandroid-intentbitmap

解决方案


因此,如果我做对了,URI 路径将存储在 uri 变量中。发生 null 错误是因为您没有初始化该 uri 字符串。你可以这样做:

String uri = new String() // -> if there is still an error here, replace String() with String(this) or String(activity_your_name.this)
Intent intent = new Intent(this, activity_output.class); // -> here you add the activity where you send the URI to
intent.putExtra("URI", uri);

在其他活动中,您可以通过以下方式提取 URI:

//Getting the URI from previous activity:
path =  getIntent().getExtra("URI");
//Initialising the File form your URI
File StringToBitmap = new File;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
StringToBitmap = new File(path, String.valueOf(options));
//Your bitmap will be stored in mBitmaps
mBitmaps = BitmapFactory.decodeFile(path);
//The next part is extra, if you want to display your bitmap into an ImageView
ImageView iv = new ImageView();
iv = findViewById(R.id.your_id);
iv.setImageBitmap(mBitmaps);

这段代码对我来说非常相似。关于那个 NULL EXCEPTION,每次你得到它时,试着看看你是否正确地初始化了你的所有对象。希望这可以帮助。


推荐阅读