android - 位图共享意图
问题描述
我正在尝试将整个 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.有没有其他方法可以做到这一点?
解决方案
因此,如果我做对了,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,每次你得到它时,试着看看你是否正确地初始化了你的所有对象。希望这可以帮助。
推荐阅读
- python - 如何在不和谐的一天中的特定时间发送消息?
- python-3.x - 如何使用 python 将 postgresql 表迁移/复制到 oracle?
- discord - 如何(自动?)将 Discord 中的一段代码标记为剧透?
- python - 如何使用 setattr() 向对象属性添加值?
- vim - 如何删除vim中的最后一个空行?
- react-native - 视频预览(来自 react-native-video)未显示
- java - Android Studio:API 调用 - 如何从 JSON 对象中读取对象
- node.js - 将 discord.js bot 转换为 exe
- php - 多个文件附件并通过表单发送
- ffmpeg - 使用 NVENC 的 ffmpeg 强调级别图