android - 通过意图发送图像

时间:2014-06-08 22:34:45

标签: android

请参阅page了解权限部分。

我有第一个应用程序,其中包含以下内容:

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):

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

我在登录接收应用中收到以下消息:

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进行测试。我使用adb将文件'earth.jpg'推送到images文件夹,可以在那里看到它。

如何解决此问题?

1 个答案:

答案 0 :(得分:0)

问题在于图像的位置。要使用files-path,需要将文件放在files /目录下。所以我的images /目录应该在那里,推送文件在:

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