简体   繁体   English

从内部存储共享图像

[英]Sharing an image from internal storage

I've been trying and trying to get this to work. 我一直在尝试并尝试使其工作。 I've gone through many examples with no luck. 我经历了很多没有运气的例子。 The problem is that the image is not attached when I try to share it, for example, using an email client. 问题是,当我尝试共享图像时(例如,使用电子邮件客户端),该图像未附加。 I managed to get this to work when using external storage but internal storage suits my needs better. 使用外部存储时,我设法使其正常工作,但内部存储更适合我的需求。

I click a button and then the image is saved to internal storage and right after that it is shared but there's no image. 我单击一个按钮,然后将图像保存到内部存储中,然后共享该图像,但是没有图像。

This is from the Activity class: 这是来自Activity类的:

int width              = size.x;
int height             = size.y;
Bitmap shareBmp        = Bitmap.createBitmap(screenBmp, 0, 0, width, height);
ContextWrapper wrapper = new ContextWrapper(getApplicationContext());
File directory         = wrapper.getDir("images", Context.MODE_PRIVATE);
File filePath          = new File(directory, "share.png");

FileOutputStream fos;

try
{
    fos = new FileOutputStream(filePath);
    shareBmp.compress(Bitmap.CompressFormat.PNG, 90, fos);
    fos.close();
}
catch (Exception aE){}

Uri uri       = Uri.parse("content://com.example.Test/share.png");
Intent intent = new Intent();

intent.setAction(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_STREAM, uri);
intent.setType("image/png");
startActivity(Intent.createChooser(intent, "Share Image"));

Here's the ImageProvider class: 这是ImageProvider类:

public class ImageProvider extends ContentProvider
{
    @Override
    public ParcelFileDescriptor openFile(Uri aUri, String aMode) throwsFileNotFoundException
    {
        File file = new File(getContext().getFilesDir(), aUri.getPath());

        if (file.exists())
        {
            return (ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY));
        }

        throw new FileNotFoundException(aUri.getPath());
    }

    @Override
    public boolean onCreate()
    {
        return false;
    }

    @Override
    public int delete(Uri aUri, String aSelection, String[] aSelectionArgs)
    {
        return 0;
    }

    @Override
    public String getType(Uri aUri)
    {
        return null;
    }

    @Override
    public Uri insert(Uri aUri, ContentValues aValues)
    {
        return null;
    }

    @Override
    public Cursor query(Uri aUri, String[] aProjection, String aSelection, String[] aSelectionArgs, String aSortOrder)
    {
        return null;
    }

    @Override
    public int update(Uri aUri, ContentValues aValues, String aSelection, String[] aSelectionArgs)
    {
        return 0;
    }
}

This is from the manifest: 这来自清单:

<provider
        android:name=".ImageProvider"
        android:authorities="com.example.Test"
        android:exported="true"/>

Only your app can see/read/write files in it's app specific -private- storage. 只有您的应用可以查看/读取/写入其特定于应用的-private-存储中的文件。 So you cannot ask other app to share from it as they cannot even 'see' those files there. 因此,您无法要求其他应用共享它,因为他们甚至无法在此处“看到”这些文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM