我正在尝试通过隐式意图共享图像。该图像已通过Messenger和Twitter成功共享,但在Facebook News Feed,Viber和Email上失败。在共享之前,我读到某个地方需要先将图像保存到外部或内部SD卡上。我对1)如何将图像保存到SD卡和2)如何指向SD卡上的新图像位置以进行共享感到迷惑不解。目前,我用于共享图像的代码如下所示:
Button share = findViewById(R.id.sharetoapps);
share.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Uri uri = Uri.parse("android.resource://com.testing.mypic/drawable/bad_day");
intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_STREAM,uri);
intent.setType("image/png");
startActivity(Intent.createChooser(intent, "share to:"));
}
});
我的xml是:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/sharetoapps"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="share" />
</LinearLayout>
如果您有任何指示或对任何相关教程的了解,我将不胜感激。
确定,我终于可以使用它了,但是它需要做很多事情。
首先,需要将以下内容添加到AndroidManifest.xml文件:
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<!-- ressource file to create -->
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths">
</meta-data>
</provider>
然后我必须在res下创建一个名为xml的文件夹。然后,我在该文件夹中创建了一个名为file_paths.xml的文件。该文件包含以下内容:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="." />
</paths>
最后,我的共享方法最终看起来像这样:
Button share = findViewById(R.id.sharetoapps);
share.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ActivityCompat.requestPermissions(QuotesActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},1);
boolean success=false;
Bitmap image = BitmapFactory.decodeResource(getResources(), getResources().getIdentifier(msg,"drawable",getPackageName()));
final String TAG = QuotesActivity.class.getSimpleName();
File path = Environment.getExternalStorageDirectory();
File dir = new File(path + "/newAppFolder/");
dir.mkdir();
File file = new File(dir,"frs.png");
OutputStream out = null;
try{
out = new FileOutputStream(file);
image.compress(Bitmap.CompressFormat.PNG, 100, out);
success=true;
out.flush();
out.close();
} catch (FileNotFoundException e){
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (success) {
Toast.makeText(getApplicationContext(), "Image saved with success",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(),
"Error during image saving", Toast.LENGTH_LONG).show();
}
File f = new File(Environment.getExternalStorageDirectory() + File.separator+ "newAppFolder" + File.separator + "frs.png");
Uri imageUri = FileProvider.getUriForFile(Objects.requireNonNull(getApplicationContext()),BuildConfig.APPLICATION_ID + ".provider", f);
Intent shareit=new Intent(Intent.ACTION_SEND);
shareit.setType("image/png");
shareit.putExtra(Intent.EXTRA_STREAM, imageUri);
startActivity(Intent.createChooser(shareit, "Share Image Via"));
startActivity(Intent.createChooser(intent, "share to:"));
}
});
注意:
[当我尝试在多个地方共享图像时,我收到警告,说我的应用程序一直在停止,并询问我是否要报告该应用程序(目前我不报告),尽管它仍允许我继续共享图片。我不确定是什么原因造成的。如果有人有主意,请告诉我。