正确的方式来分享影像(使用意图)意图、影像、正确、方式

2023-09-07 23:38:07 作者:╚橘色半夏╝

我在我的应用程序创建的图像,并希望分享这些社交网络(脸谱),邮件应用程序(Gmail的),和其他应用程序,可以接收的图像。

问题(我认为)的起源是,我不希望使用外部存储作为我的图片基地。我希望用我的数据文件夹或我的缓存文件夹,因为这些都不需要访问的许可。

在code,我用它来写我的图像文件(和我指定的 MODE_WORLD_READABLE ,以便其他应用程序可以读取它们):

 的FileOutputStream FOS = NULL;尝试{    FOS = context.openFileOutput(image.jpg的Context.MODE_WORLD_READABLE);    bitmap.com preSS(Bitmap.Com pressFormat.JPEG,100,FOS);} {最后    如果(FOS!= NULL)        fos.close();} 

这是code,我将影像分享的:

 文件internalFile = context.getFileStreamPath(image.jpg文件);意向意图=新的Intent();intent.setAction(Intent.ACTION_SEND);intent.putExtra(Intent.EXTRA_STREAM,Uri.fromFile(internalFile));intent.setType(图像/ JPEG);intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);context.startActivity(Intent.createChooser(意向,共享)); 

此解决方案是很容易的,像Fac​​ebook应用程序工作正常,但不会作为它与failes例如Gmail的:

 文件://附件路径必须指向的file:/// MNT / SD卡 
设计模式的UML图

有一些黑客(见下文),以获得它与Gmail的工作,但我让我问自己,如果有一个更好的方式来分享照片,如果没有黑客的作品,这是我忽视。所以,问题:

什么是分享图像的最佳方法是什么? (外部存储?)有没有更多的应用程序,(误)的行为就像Gmail吗? (我已经看到了一些麻烦与谷歌+)的如果没有其他的方式:的我可以写特殊意图分享到特定的应用程序。我有一个共享的默认方式,当用户我的观察名单中选择一个应用程序覆盖它?

黑客

使用路径黑客 按简单地指向乌里来:

文件:/// MNT / SD卡/../../我/包/名/ ...

该解决方案感觉不对。

使用的ContentProvider 描述的此处。但是从链接引用

  

警告:在帖子中描述的方法适用于Gmail,但显然与其他ACTION_SEND处理一些问题(如彩信作曲家)。

的(问题:它崩溃彩信作曲)的

解决方案

您应该使3个步骤。结果拍照。

 公共位图takeScreenshot(){    查看rootView = findViewById(android.R.id.content).getRootView();    rootView.setDrawingCacheEnabled(真);    返回rootView.getDrawingCache();} 

保存图片。

 公共字符串saveBitmap(位图位图){文件的ImagePath =新的文件(Environment.getExternalStorageDirectory()+/screenshot.png);    FileOutputStream中FOS;    尝试{        FOS =新的FileOutputStream(的ImagePath);        bitmap.com preSS(比较pressFormat.PNG,100,FOS);        fos.flush();        fos.close();    }赶上(FileNotFoundException异常五){        Log.e(GREC,e.getMessage(),E);    }赶上(IOException异常五){        Log.e(GREC,e.getMessage(),E);    }    返回imagePath.getAbsolutePath();} 

分享到社交网络。

I create images in my app and want to share these social networks (facebook), mail apps (gmail), and other apps that can "receive" images.

The origin of the problem (I think) is that I don't want to use the external storage as a base for my images. I want to either use my data folder or my cache folder since neither of these require any permission to access.

The code which I use to write my image to file (and I specify the MODE_WORLD_READABLE so that other apps can read them):

FileOutputStream fos = null;
try {
    fos = context.openFileOutput("image.jpg", Context.MODE_WORLD_READABLE);
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
} finally {
    if (fos != null)
        fos.close();
}

And this is the code where I share the image:

File internalFile = context.getFileStreamPath("image.jpg");

Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(internalFile));
intent.setType("image/jpeg");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

context.startActivity(Intent.createChooser(intent, "share"));

This solution is very easy and works fine for apps like facebook but not for example gmail which failes with:

file:// attachment paths must point to file:///mnt/sdcard

There are a number of "hacks" (see below) to get it to work with gmail but I leaves me asking myself if there is an even better way to share images that works without hacks, something I overlooked. So, to the questions:

What is the best way to share images? (external storage?) Is there any more apps that (mis-)behave just like gmail? (I have seen some trouble with google+) If there is no other way: Can I write special intents for sharing to specific apps. I have a default way of sharing and override it when the user selects an app on my watch list?

Hacks

Using a path-hack by simply pointing the Uri to:

file:///mnt/sdcard/../../my/package/name/...

This solution doesn't feel right.

Using a ContentProvider as described here. But quoted from the link:

Warning: the method described in the post works well for Gmail, but apparently has some issues with other ACTION_SEND handlers (e.g. the MMS composer).

(Issue: It crashes the MMS composer)

解决方案

You should to make 3 steps. Take picture.

public Bitmap takeScreenshot() {
    View rootView = findViewById(android.R.id.content).getRootView();
    rootView.setDrawingCacheEnabled(true);
    return rootView.getDrawingCache();
}

Save picture.

public String saveBitmap(Bitmap bitmap) {

File imagePath = new File(Environment.getExternalStorageDirectory() + "/screenshot.png");
    FileOutputStream fos;
    try {
        fos = new FileOutputStream(imagePath);
        bitmap.compress(CompressFormat.PNG, 100, fos);
        fos.flush();
        fos.close();
    } catch (FileNotFoundException e) {
        Log.e("GREC", e.getMessage(), e);
    } catch (IOException e) {
        Log.e("GREC", e.getMessage(), e);
    }

    return imagePath.getAbsolutePath();
} 

Share to social network.