如何使用文件提供程序从适配器分享图像

huangapple go评论69阅读模式
英文:

How to share image from adapter using file provider

问题

我想要分享的图片存储在drawable中。
我通过适配器读取图像,因为我创建了图像的数组列表。
我正在尝试使用文件提供程序分享图像。如果我想通过Gmail或WhatsApp分享,我将无法看到图像。
如何从哈希映射"iImage"中获取图像?

在MainActivity中,我为drawable中的图像列表创建了int变量

// 在位置上的歌曲标题的ListView图标
icon = new int[]{R.drawable.flower1, R.drawable.flower2, R.drawable.flower3, R.drawable.flower4, R.drawable.flower5, R.drawable.flower6, R.drawable.flower7, R.drawable.flower8, R.drawable.flower9, R.drawable.flower10};

这是将图像发送到DetailActivity的适配器中的内容

public void onBindViewHolder(@NonNull final MyHolder7 myHolder, int i) {
    // ...
    myHolder.mImageView.setImageResource(models.get(i).getIcon());
    // ...
    myHolder.setItemCLickListener(new ItemClickListener7() {
        @Override
        public void onItemClickListener(View v, int position) {
            // ...
            int imageId = models.get(position).getIcon();
            // ...
        }
    });
}

DetailActivity.java中显示要使用文件提供程序分享的图像:

imageView = findViewById(R.id.ImageFoot1);
if (intent != null && !intent.getExtras().isEmpty()) {
    imageResourceId = intent.getIntExtra("iImage", -1);
    imageView.setImageResource(imageResourceId);
}

以下是我尝试但失败的分享方式

AndroidManifest.xml中进行以下配置:

<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_path" />
</provider>

这是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>

这是我的分享代码。如何修复通过适配器显示图像的确切哈希映射,例如:imageResourceId = intent.getIntExtra("iImage", -1);,以便它能够通过适配器中的位置读取图像。

buttonShare = findViewById(R.id.btnImageShare);
buttonShare.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Drawable drawable = imageView.getDrawable();
        Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
        try {
            File file = new File(getApplicationContext().getExternalCacheDir(), File.separator + "iImage");
            // ...
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
});
英文:

The images i want to share are stored in the drawable.
I read the image through adapter as i made arraylist of images.
I am trying to share image using file provider. If i want to share with maybe Gmail or WhatsApp etc, i will not see image.
How do source the image from hashmap "iImage"

In MainActivity i made int for list of images in drawable

//Listview icons for song titles in position
icon = new int[]{R.drawable.flower1, R.drawable.flower2, R.drawable.flower3, R.drawable.flower4, R.drawable.flower5, R.drawable.flower6, R.drawable.flower7, R.drawable.flower8, R.drawable.flower9, R.drawable.flower10};

This is what i have in the Adapter that sends the image to DetailActivity

    public void onBindViewHolder(@NonNull final MyHolder7 myHolder, int i) {

        myHolder.mTitle.setText(models.get(i).getTitle()); //here is position
        myHolder.mDesc.setText(models.get(i).getDesc());
        myHolder.mImageView.setImageResource(models.get(i).getIcon());
        myHolder.mSubImageView.setImageResource(models.get(i).getIcon2());// here we used imge resource

        myHolder.setItemCLickListener(new ItemClickListener7() {
            @Override
            public void onItemClickListener(View v, int position) {

                String gTitle = models.get(position).getTitle();
                String gDesc = models.get(position).getDesc();
                int imageId = models.get(position).getIcon();



                //get our data with intent
                Intent intent = new Intent(mContext, DetailActivity7.class);
                intent.putExtra(&quot;actionBarTitle&quot;, models.get(position).getTitle());
                intent.putExtra(&quot;brandNewDesc&quot;, models.get(position).getBrandNewDesc());
                intent.putExtra(&quot;iImage&quot;, imageId);
                mContext.startActivity(intent);
                return;
            }
        });

   }

This is what displayed the image in the DetailActivity.java i want to share using file provider

imageView = findViewById(R.id.ImageFoot1);
        if (intent != null &amp;&amp; !intent.getExtras().isEmpty()) {
            imageResourceId = intent.getIntExtra(&quot;iImage&quot;, -1);
            imageView.setImageResource(imageResourceId);
        }

NOW THIS IS HOW I SHARED THAT FAILED ME

I have configured the Manifest this below

&lt;provider
            android:name=&quot;androidx.core.content.FileProvider&quot;
            android:authorities=&quot;${applicationId}.provider&quot;
            android:exported=&quot;false&quot;
            android:grantUriPermissions=&quot;true&quot;&gt;
            &lt;meta-data
                android:name=&quot;android.support.FILE_PROVIDER_PATHS&quot;
                android:resource=&quot;@xml/provider_path&quot; /&gt;
        &lt;/provider&gt;

This is the xml/file_paths file:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;paths xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;&gt;
    &lt;external-path name=&quot;external_files&quot; path=&quot;.&quot;/&gt;
&lt;/paths&gt;

And this is my share Code How do fix the exact hashmap that displayed the image through adapter for example this: imageResourceId = intent.getIntExtra(&quot;iImage&quot;, -1); so that it will read the mage through the position through the adapter.

 buttonShare = findViewById(R.id.btnImageShare);
        buttonShare.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Drawable drawable=imageView.getDrawable();
                Bitmap bitmap=((BitmapDrawable)drawable).getBitmap();

                try {
                    File file = new File(getApplicationContext().getExternalCacheDir(), File.separator +&quot;iImage&quot;);
                    FileOutputStream fOut = new FileOutputStream(file);
                    bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
                    fOut.flush();
                    fOut.close();
                    file.setReadable(true, false);
                    final Intent intent = new Intent(android.content.Intent.ACTION_SEND);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    Uri photoURI = FileProvider.getUriForFile(getApplicationContext(), BuildConfig.APPLICATION_ID +&quot;.provider&quot;, file);

                    intent.putExtra(Intent.EXTRA_STREAM, photoURI);
                    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                    intent.setType(&quot;image/png&quot;);

                    startActivity(Intent.createChooser(intent, &quot;Share image via&quot;));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });


    }

答案1

得分: 1

是的!我做对了!!!我现在非常开心。始终努力以实现目标是很好的。

如果您正在使用Recylerview Adapter将图像完整地传递到DetailActivity,这个解决方案将帮助您共享在DetailActivity中收到的图像,以便您可以与社交媒体应用程序共享。

以下是我如何通过意图从Adapter接收图像并将其放入DetailActivity中:

imageView = findViewById(R.id.ImageFoot1);
if (intent != null && !intent.getExtras().isEmpty()) {
    imageResourceId = intent.getIntExtra("iImage", -1);
    imageView.setImageResource(imageResourceId);
}

以下是正确的图像共享代码:

Drawable drawable = imageView.getDrawable();
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();

try {
    File file = new File(getApplicationContext().getExternalCacheDir(), File.separator + "iImage" + imageResourceId + ".png");
    FileOutputStream fOut = new FileOutputStream(file);
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
    fOut.flush();
    fOut.close();
    file.setReadable(true, false);
    final Intent intent = new Intent(android.content.Intent.ACTION_SEND);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    Uri photoURI = FileProvider.getUriForFile(getApplicationContext(), BuildConfig.APPLICATION_ID + ".provider", file);
    intent.putExtra(Intent.EXTRA_STREAM, photoURI);
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    intent.setType("image/png");
    startActivity(Intent.createChooser(intent, "通过以下方式分享图片"));
} catch (Exception e) {
    e.printStackTrace();
}

祝你好运!

英文:

Yes! I have got it right!!! I'm very happy now. It is good to always apply effort so as to achieve aim.

If you are using Recylerview Adapter to pass image in full to DetailActivity, this solution will help you share the image received in DetailActivity so that you can share with social media apps.

This is how i received the image from Adapter with intent put extrass into DetailActivity

imageView = findViewById(R.id.ImageFoot1);
        if (intent != null &amp;&amp; !intent.getExtras().isEmpty()) {
            imageResourceId = intent.getIntExtra(&quot;iImage&quot;, -1);
            imageView.setImageResource(imageResourceId);
        }

This is the Correct Image Share Code

Drawable drawable=imageView.getDrawable();
                Bitmap bitmap=((BitmapDrawable)drawable).getBitmap();

                try {
                    File file = new File(getApplicationContext().getExternalCacheDir(), File.separator +&quot;iImage&quot;+imageResourceId+&quot;.png&quot;);
                    FileOutputStream fOut = new FileOutputStream(file);
                    bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
                    fOut.flush();
                    fOut.close();
                    file.setReadable(true, false);
                    final Intent intent = new Intent(android.content.Intent.ACTION_SEND);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    Uri photoURI = FileProvider.getUriForFile(getApplicationContext(), BuildConfig.APPLICATION_ID +&quot;.provider&quot;, file);
                    intent.putExtra(Intent.EXTRA_STREAM, photoURI);
                    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                    intent.setType(&quot;image/png&quot;);
                    startActivity(Intent.createChooser(intent, &quot;Share image via&quot;));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

Goodluck!

答案2

得分: 0

抛出 FileNotFoundException,并且权限访问被拒绝。

2020-08-15 07:29:01.103 10745-10745/com.joseph.imagesshareW/System.err: java.io.FileNotFoundException: /storage/emulated/0/Download/share_image_1597472941101.png(权限被拒绝)

获取用户对存储的权限

在您的清单文件中

&lt;uses-permission android:name=&quot;android.permission.WRITE_EXTERNAL_STORAGE&quot;/&gt;
&lt;uses-permission android:name=&quot;android.permission.READ_EXTERNAL_STORAGE&quot;/&gt;

请求运行时权限 阅读如何请求权限的详细信息 这里是代码
用于请求运行时权限

或者您可以在设置中手动为应用程序授予权限,然后尝试
希望能够起作用

英文:

It throws FileNotFoundException and the permission access is denied

2020-08-15 07:29:01.103 10745-10745/com.joseph.imagesshareW/System.err: java.io.FileNotFoundException: /storage/emulated/0/Download/share_image_1597472941101.png (Permission denied)

get user pemission for storage

In your manifest file

&lt;uses-permission android:name=&quot;android.permission.WRITE_EXTERNAL_STORAGE&quot;/&gt;
&lt;uses-permission android:name=&quot;android.permission.READ_EXTERNAL_STORAGE&quot;/&gt;

ask runtime permission read through for how to request permission Here the code
For Asking runtime permission

Or you can give manually permission for app in setting then try
I hope it will work

huangapple
  • 本文由 发表于 2020年8月15日 03:56:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/63419288.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定