如何从文件资源管理器在我的应用程序中打开单个图像
How to open a single image in my app from file explorer
我正在开发一个应用程序,我想在其中从外部文件资源管理器打开我的应用程序中的任何照片。就像当我点击照片时,选择应用程序 window 出现并且我 select 我的应用程序和图像显示在我的应用程序中。但是在这里,没有图像出现。
我在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("-----------------------------");
}
}
}
这是我的截图:
问题是您试图通过错误的方式获取 Uri
。更改
的所有出现
Uri receivedUri = (Uri) receivedIntent.getParcelableExtra(Intent.EXTRA_STREAM);
到
Uri receivedUri = receivedIntent.getData();
这将使您获得文件的权利 Uri
。还要注意 setImageURI
确实
Bitmap reading and decoding on the UI thread
参考this link了解更多详情。
我正在开发一个应用程序,我想在其中从外部文件资源管理器打开我的应用程序中的任何照片。就像当我点击照片时,选择应用程序 window 出现并且我 select 我的应用程序和图像显示在我的应用程序中。但是在这里,没有图像出现。
我在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("-----------------------------");
}
}
}
这是我的截图:
问题是您试图通过错误的方式获取 Uri
。更改
Uri receivedUri = (Uri) receivedIntent.getParcelableExtra(Intent.EXTRA_STREAM);
到
Uri receivedUri = receivedIntent.getData();
这将使您获得文件的权利 Uri
。还要注意 setImageURI
确实
Bitmap reading and decoding on the UI thread
参考this link了解更多详情。