位图共享意图

时间:2018-07-16 10:52:35

标签: android android-intent bitmap

我正在尝试将整个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的Height和Width的硬编码值,则它可以正常工作,但是当我使用

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

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

查询

1。我该如何运作?

2。还有其他方法吗?

2 个答案:

答案 0 :(得分:0)

因此,如果我理解正确,则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,每次获得它时,请尝试查看是否正确初始化了所有对象。希望这会有所帮助。

答案 1 :(得分:0)

尝试一下:

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);