Android的 - 通过发送意向形象 [英] android - sending image via intent

查看:158
本文介绍了Android的 - 通过发送意向形象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请参照<一个href=\"http://developer.android.com/reference/android/support/v4/content/FileProvider.html#Permissions\"相对=nofollow>许可的部分页面。

我有具有以下的第一个应用程序:

I have the first app which has the following :

File imagePath = new File(getApplicationContext().getFilesDir(), "images");
File newFile = new File(imagePath, "earth.jpg");
Uri contentUri = FileProvider.getUriForFile(getApplicationContext(),
                        "com.android.provider.DataSharing", newFile);

getApplicationContext().grantUriPermission("com.android.datasharing_2.app", contentUri, Intent.FLAG_GRANT_READ_URI_PERMISSION);

Intent i = new Intent();
i.setAction(Intent.ACTION_SEND);
i.putExtra(Intent.EXTRA_STREAM, contentUri);
i.setType("image/jpg");
startActivity(i);

在清单:

<provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="com.android.provider.DataSharing"
        android:exported="false"
        android:grantUriPermissions="true">

        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/paths"/>

</provider> 

在接收应用程序(com.android.datasharing_2.app):

In the receiving app (com.android.datasharing_2.app) :

Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
if (imageUri != null){
    ImageView iv = (ImageView) findViewById(R.id.imageView);
    iv.setImageURI(imageUri);
}

我得到了登录以下消息接收应用程序:

I get the following message in Log in receiving app:

resolveUri failed on bad bitmap uri: content://com.android.provider.DataSharing/my_images/earth.jpg

 java.io.FileNotFoundException: No such file or directory
        at android.database.DatabaseUtils.readExceptionWithFileNotFoundExceptionFromParcel(DatabaseUtils.java:146)
        at android.content.ContentProviderProxy.openTypedAssetFile(ContentProviderNative.java:682)
        at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1063)
        at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:904)
        at android.content.ContentResolver.openInputStream(ContentResolver.java:629)
        at android.widget.ImageView.resolveUri(ImageView.java:659)
        at android.widget.ImageView.setImageURI(ImageView.java:399)

我使用的是Genymotion VM测试。我的文件'earth.jpg推到图像文件夹使用亚行和可以在那里看到了。

I am testing using the Genymotion VM. I push the file 'earth.jpg' to the images folder using adb and can see it there.

我如何解决此问题?

推荐答案

的问题是与图像的位置。要使用文件路径,需要把文件的文件/目录下。所以,我的images /目录应该已经在那里,与推文件时间:

The problem was with the location of the image. To use files-path, one needs to put files under files/ directory. So my images/ directory should have been under there, with the pushed file at :

app-dir-path/files/images/earth.jpg

这篇关于Android的 - 通过发送意向形象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆