首页 > 解决方案 > 我的应用需要在 SD 卡上存储一张图片以在 Facebook 动态消息上分享

问题描述

我试图通过隐含的意图分享图像。该图像已通过 Messenger 和 Twitter 成功共享,但在 Facebook 新闻提要、Viber 和电子邮件上失败。我在某处读到需要先将图像保存到外部或内部 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>

如果您有任何指示或知道任何相关教程,我将不胜感激。

标签: androidandroid-sdcardandroid-implicit-intent

解决方案


好的,我终于让它工作了,但它需要很多东西。

首先需要将以下内容添加到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:"));
            }
        });

笔记:

当我尝试在多个地方共享图像时,我收到一条警告,说我的应用程序不断停止并询问我是否要报告该应用程序(我目前不这样做),尽管它仍然允许我继续共享图像. 我不确定是什么原因造成的。如果有人有想法,请告诉我。


推荐阅读