【问题标题】:Bitmap Share Intent位图共享意图
【发布时间】:2018-07-16 10:52:35
【问题描述】:

我正在尝试将整个 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" />

卡片视图 XML 如果我给 CardView 一个定义的 CardView 的高度和宽度的硬编码值,它正在工作,但是当我使用时

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

上面显示的代码给出了 NULL_POINTER_EXCEPTION。

查询

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

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

【问题讨论】:

  • 宽度和高度分别返回 1158 和 304。
  • 我猜你弄错了。在每种情况下,我都会得到这些值,但在每种情况下,我也会得到路径 NULL。

标签: android android-intent bitmap


【解决方案1】:

所以如果我做对了,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,每次你得到它时,试着看看你是否正确地初始化了你的所有对象。希望这会有所帮助。

【讨论】:

    【解决方案2】:

    试试这个:

    public Bitmap getScreenShot(View view) {
            view.setDrawingCacheEnabled(true);
            Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
            view.setDrawingCacheEnabled(false);
            return bitmap;
        }
    
    public void onShareImage(Bitmap bitmap) {
        @SuppressLint("SimpleDateFormat") String fileName = "IMG_" + new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()) + ".PNG";
        storeScreenShot(bitmap, fileName);
        Uri imgUri = Uri.fromFile(new File(VarName.SCREENSHOT_DIRECTORY + "/" + fileName));
    
        StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
        StrictMode.setVmPolicy(builder.build());
    
        Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
        shareIntent.setType("image/*");
        shareIntent.putExtra(Intent.EXTRA_STREAM, imgUri);
        shareIntent.putExtra(Intent.EXTRA_TEXT, VarName.DRESS_ME_SHARE_MASSAGE_HEADER + "\n" + VarName.DRESS_ME_SHARE_APP_LINK);
        startActivity(Intent.createChooser(shareIntent, "Share Image"));
    }
    
    public void storeScreenShot(Bitmap bm, String fileName) {
    
        File dir = new File(VarName.SCREENSHOT_DIRECTORY);
        if (!dir.exists()) {
            dir.mkdirs();
            File file = new File(dir.getAbsolutePath(), ".nomedia");
            try {
                file.createNewFile();
                final Intent scanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                final Uri contentUri = Uri.fromFile(file);
                scanIntent.setData(contentUri);
                sendBroadcast(scanIntent);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        File file = new File(VarName.SCREENSHOT_DIRECTORY, fileName);
        try {
            FileOutputStream fOut = new FileOutputStream(file);
            bm.compress(Bitmap.CompressFormat.PNG, 100, fOut);
            fOut.flush();
            fOut.close();
            file.createNewFile();
            final Intent scanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
            final Uri contentUri = Uri.fromFile(file);
            scanIntent.setData(contentUri);
            sendBroadcast(scanIntent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    

    现在你只需要调用函数。

    Bitmap bitmap = getScreenShot(cardView);
    onShareImage(bitmap);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-03
      • 2016-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多