4

我正在尝试将卡片视图转换为图像并通过什么应用程序共享它。我正在使用文件提供程序,因为测试设备的版本更高。

所以当我试图创建一个文件时,它给了我一个例外

e = {IllegalArgumentException@7727} "java.lang.IllegalArgumentException: Failed to find configured root that contains /com.example.onboardingversion2/images/card_image"
mCardFile = {File@7701} "com.example.onboardingversion2/images/card_image"

对于代码:

   public void createFile() {

        FileOutputStream outputStream = null;
        try {

      File imagePath = new File(getActivity().getPackageName(), "images");

            mCardFile = new File(imagePath, "card_image");

            Uri contentUri = getUriForFile(getContext(), getActivity().getPackageName() + ".provider", mCardFile);
            sendCard(contentUri);

        } catch (Exception e) {
            e.printStackTrace();
        }
}

当我使用 getFilesDir() 使用下面的代码时,它只会进入应用程序意图,当我尝试共享时没有显示图像。

     File imagePath = new File(getActivity().getFilesDir(), "images");
            mCardFile = new File(imagePath, "card_image");

            Uri contentUri = getUriForFile(getContext(), getActivity().getPackageName() + ".provider", mCardFile);
            sendCard(contentUri);

xml文件

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <files-path name="my_images" path="images/"/>
</paths>

显现

     <provider
        android:name="androidx.core.content.FileProvider"
        android:authorities="${applicationId}.provider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths" />
    </provider>

将视图转换为位图

 public Bitmap viewToBitmap(final View view) {

    cardView.post(new Runnable() {
        @Override
        public void run() {
            mBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(mBitmap);
            view.draw(canvas);

            createFile();

        }
    });

    return mBitmap;
}

分享代码:

public void sendCard(Uri contentUri)
{

    Intent whatsappIntent = new Intent(Intent.ACTION_SEND);
    whatsappIntent.setType("text/plain");
    whatsappIntent.setPackage("com.whatsapp");
    whatsappIntent.putExtra(Intent.EXTRA_TEXT, "The text you wanted to share");
    whatsappIntent.putExtra(Intent.EXTRA_STREAM, contentUri);
    whatsappIntent.setType("image/jpeg");
    whatsappIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

    try {
        getActivity().startActivity(whatsappIntent);
    } catch (android.content.ActivityNotFoundException ex) {
        Utils.showDialog(getActivity(),"Whats App have not been installed.");
    }
}

我有一个卡片视图,我正在尝试将其创建为位图并将位图转换为图像文件并使用共享意图进行共享。

我哪里错了?视图是否无法转换为图像?

请帮忙。谢谢你。

4

2 回答 2

2

首先添加权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

使用来自 res 的位图

Bitmap b =BitmapFactory.decodeResource(getResources(),R.drawable.userimage);
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(getContentResolver(),
                b, "Title", null);
Uri imageUri =  Uri.parse(path);
share.putExtra(Intent.EXTRA_STREAM, imageUri);
startActivity(Intent.createChooser(share, "Select"));
于 2019-04-02T09:54:14.317 回答
2

这是一个没有不必要权限的简单方法:

  1. 在您的 res 文件夹中创建 xml 文件夹(右键单击 res -> new -> android 资源目录 -> 资源类型:xml)。

  2. 创建名为 file_paths.xml 的新 xml 文件

  3. 将此代码粘贴到里面

文件路径.xml:

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <cache-path name="Download" path="." />
</paths>

在您的清单中,在应用程序下粘贴此行:

    <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="${applicationId}.my.package.name.provider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths" />
    </provider>

在你的课堂上:

public void sendViewViaMail(final View view, final Context baseContext, final Context activityContextOnly, final String textToMail) {
    view.post(new Runnable() {
        @Override
        public void run() {
            int heightG = view.getHeight();
            int widthG = view.getWidth();
            sendViewViaMail(view, baseContext, activityContextOnly, widthG, heightG, textToMail);
        }
    });
}


private void sendViewViaMail(View view, final Context baseContext, Context activityContextOnly, int widthG, int heightG, String textToMail) {
    Bitmap bitmap = MyBitmapUtils.createViewBitmap(view, widthG, heightG);
    Uri imageUri = null;

        File file = null;
        FileOutputStream fos1 = null;
        try {
            File folder = new File(activityContextOnly.getCacheDir() + File.separator + "My Temp Files");

            boolean success = true;
            if (!folder.exists()) {
                success = folder.mkdir();
            }

            String filename = "img.jpg";
            file = new File(folder.getPath(), filename);

            fos1 = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos1);

            imageUri = FileProvider.getUriForFile(activityContextOnly, activityContextOnly.getPackageName() + ".my.package.name.provider", file);
        } catch (Exception ex) {

        } finally {
            try {
                fos1.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    final Intent emailIntent1 = new Intent(Intent.ACTION_SEND);
    emailIntent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    emailIntent1.putExtra(Intent.EXTRA_EMAIL, new String[]{});
    emailIntent1.putExtra(Intent.EXTRA_STREAM, imageUri);
    emailIntent1.putExtra(Intent.EXTRA_SUBJECT, "[" + "COMPANY_HEADER" + "]");
    emailIntent1.putExtra(Intent.EXTRA_TEXT, textToMail);
    emailIntent1.setData(Uri.parse("mailto:" + "mail@gmail.com")); // or just "mailto:" for blank
    emailIntent1.setType("image/jpg");
    activityContextOnly.startActivity(Intent.createChooser(emailIntent1, "Send email using"));
}

public static Bitmap createViewBitmap(View view, int widthG, int heightG) {
    Bitmap viewBitmap = Bitmap.createBitmap(widthG, heightG, Bitmap.Config.RGB_565);
    Canvas viewCanvas = new Canvas(viewBitmap);
    Drawable backgroundDrawable = view.getBackground();

    if(backgroundDrawable!=null){
        backgroundDrawable.draw(viewCanvas);
    }
    else{
        viewCanvas.drawColor(Color.WHITE);
        view.draw(viewCanvas);
    }
    return viewBitmap;
}

如何调用它:

CardView myCard = (CardView) findViewById(R.id.myCard);
sendViewViaMail(myCard, getApplicationContext(), Activity_Cabinet.this, report);
于 2019-04-02T10:14:14.590 回答