如何在文件资源管理器

时间:2016-03-31 07:11:13

标签: 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

1 个答案:

答案 0 :(得分:1)

问题是你试图以错误的方式获得Uri。改变所有出现的

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

 Uri receivedUri = receivedIntent.getData();

这将为您提供正确的Uri文件。另请注意setImageURI

  

UI线程上的位图读取和解码

有关详细信息,请参阅this链接。