android - 如何从文件资源管理器在我的应用程序中打开单个图像

标签 android android-intent android-imageview intentfilter

我正在开发一个应用程序,我想在该应用程序中从外部文件资源管理器打开我的应用程序中的任何照片。就像当我点击照片时,会出现选择应用程序窗口,然后我选择我的应用程序,图像就会显示在我的应用程序中。但是在这里,没有图像出现。

我在Manifest.xml中添加了代码:

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
        <activity
            android:name="com.image.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.SEND" />
                <action android:name="android.intent.action.VIEW" />
                <action android:name="android.intent.action.EDIT" />

                <category android:name="android.intent.category.BROWSABLE" />
                <category android:name="android.intent.category.DEFAULT" />

                <data android:mimeType="image/*" />
                <data android:mimeType="text/*" />
            </intent-filter>
        </activity>
</application>

布局文件activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    tools:context=".DataReceiverActivity" >

    <ImageView
        android:id="@+id/picture"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:contentDescription="Receive Picture Sharing" />

    <TextView
        android:id="@+id/txt"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</RelativeLayout>

在我的 MainActivity.java 中:

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ImageView picView = (ImageView) findViewById(R.id.picture);
        TextView txtView = (TextView) findViewById(R.id.txt);

        Intent receivedIntent = getIntent();
        String receivedAction = receivedIntent.getAction();
        String receivedType = receivedIntent.getType();

        // make sure it's an action and type we can handle
        if (receivedAction.equals(Intent.ACTION_SEND)) {
            if (receivedType.startsWith("text/")) {
                picView.setVisibility(View.GONE);
                String receivedText = receivedIntent
                    .getStringExtra(Intent.EXTRA_TEXT);
                if (receivedText != null) {
                    txtView.setText(receivedText);
                }
            } else if (receivedType.startsWith("image/")) {
                txtView.setVisibility(View.GONE);
                Uri receivedUri = (Uri) receivedIntent
                    .getParcelableExtra(Intent.EXTRA_STREAM);
                if (receivedUri != null) {
                    picView.setImageURI(receivedUri);// just for demonstration
                }
            }
        } else if (receivedAction.equals(Intent.ACTION_VIEW)) {

            if (receivedType.startsWith("text/")) {
                Toast.makeText(this, "TextRecived", Toast.LENGTH_SHORT).show();
                Uri uri2 = receivedIntent.getData();
                String uri = uri2.getEncodedPath() + "  complete: "
                    + uri2.toString();
                txtView.setText(uri);
            } else if (receivedType.startsWith("image/")) {

                txtView.setVisibility(View.GONE);
                Uri receivedUri = (Uri) receivedIntent
                    .getParcelableExtra(Intent.EXTRA_STREAM);
                Toast.makeText(this, receivedUri.toString(), Toast.LENGTH_SHORT).show();
                if (receivedUri != null) {
                    picView.setImageURI(receivedUri);// just for demonstration
                }
            }
        } else if (receivedAction.equals(Intent.ACTION_MAIN)) {
            txtView.setText("-----------------------------");
        }
    }

}

这是我的截图:

opening image from explorer when selecting my app empty screen appare

最佳答案

问题是您试图通过错误的方式获取 Uri。更改所有出现的

Uri receivedUri = (Uri) receivedIntent.getParcelableExtra(Intent.EXTRA_STREAM);

 Uri receivedUri = receivedIntent.getData();

这将为您提供文件的正确 Uri。还要注意 setImageURI 确实

Bitmap reading and decoding on the UI thread

引用this链接了解更多详情。

关于android - 如何从文件资源管理器在我的应用程序中打开单个图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36326478/

相关文章:

Android:使用 Intent 共享纯文本(到所有消息传递应用程序)

java - Intent 在 android N 上安装 apk

android - 如何设置放置在 ImageView 上的 TextView 的背景不透明度

android - 如何使用 Sphero 使用 CollisionDetectedAsyncData 模拟物理世界中的 2D 碰撞?

android - 在不启动 Activity 的情况下将 Intent 从 GcmIntentService 发送到当前 Activity

android - imageview android的波浪动画

android - 有图像的 imageview 的快照

android - 数据库更新时应用崩溃

java - 为什么 setImageResource 方法不起作用?

android - 我调用 finish() 和 startActivity() 的顺序有关系吗?