我正在尝试将卡片视图转换为图像并通过whats应用程序共享。我正在使用文件提供程序,因为测试设备的版本更高。
因此,当我尝试创建文件时,它给了我一个例外
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.");
}
}
我有一个卡片视图,我正在尝试将其创建为位图,并将位图转换为图像文件并使用共享意图进行共享。
我哪里出错了?视图无法转换为图像吗?
请帮忙。谢谢。
首先添加权限
<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"));
这是一种没有必要权限的简单方法:
-
在您的 res 文件夹中创建 XML 文件夹(右键单击 res -> 新建 -> android 资源目录 ->资源类型:XML(。
-
创建名为 file_paths.xml 的新 XML 文件
-
将此代码粘贴在里面
file_paths.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);