我正在开发2个应用程序,一个用于发送图像,另一个用于接收它。我在发送图像文件或位图时遇到同样的问题。 我已经在这里阅读了很多Q& A,但没有任何帮助。
1st App Manifest
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application ...>
<activity ...>
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
第一个App类(使用Bitmap)
try {
//Write file
String filename = "bitmap.png";
FileOutputStream stream = this.openFileOutput(filename, Context.MODE_PRIVATE);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
//Cleanup
stream.close();
bitmap.recycle();
//Pop intent
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/*");
intent.putExtra("image", filename);
startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}
第二个App Manifest
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application ...>
<activity ...>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
</intent-filter>
</activity>
</application>
第二个App类
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType();
imageView = (ImageView) findViewById(R.id.imageView);
if (Intent.ACTION_SEND.equals(action) && type != null) {
if (type.startsWith("image/")) {
Bitmap bmp = null;
String filename = intent.getStringExtra("image");
try {
FileInputStream is = this.openFileInput(filename);
bmp = BitmapFactory.decodeStream(is);
is.close();
imageView.setImageBitmap(bmp);
} catch (Exception e) {
e.printStackTrace();
}
}
}
错误总是有的,当我尝试从文件发送uri时我也会犯同样的错误。 (我想是因为我无法在第二个应用程序中打开文件,但我不知道它是多么开放,我试图保存并在很多地方打开但没有任何帮助)
03-22 14:52:05.412 1286-1818/? I/ActivityManager: START u0 {act=android.intent.action.SEND typ=image/* cmp=com.example.k.handleintents/.MainActivity (has extras)} from uid 10064 on display 0
03-22 14:52:05.467 3069-3069/? W/System.err: java.io.FileNotFoundException: /data/user/0/com.example.k.handleintents/files/bitmap.png: open failed: ENOENT (No such file or directory)
03-22 14:52:05.467 3069-3069/? W/System.err: at libcore.io.IoBridge.open(IoBridge.java:452)
03-22 14:52:05.467 3069-3069/? W/System.err: at java.io.FileInputStream.<init>(FileInputStream.java:76)
03-22 14:52:05.467 3069-3069/? W/System.err: at android.app.ContextImpl.openFileInput(ContextImpl.java:384)
03-22 14:52:05.468 3069-3069/? W/System.err: at android.content.ContextWrapper.openFileInput(ContextWrapper.java:177)
03-22 14:52:05.468 3069-3069/? W/System.err: at com.example.jorgealberto.handleintents.MainActivity.handleSendImage(MainActivity.java:85)
03-22 14:52:05.468 3069-3069/? W/System.err: at com.example.jorgealberto.handleintents.MainActivity.onCreate(MainActivity.java:46)
03-22 14:52:05.468 3069-3069/? W/System.err: at android.app.Activity.performCreate(Activity.java:6237)
03-22 14:52:05.468 3069-3069/? W/System.err: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
03-22 14:52:05.468 3069-3069/? W/System.err: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
03-22 14:52:05.468 3069-3069/? W/System.err: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
03-22 14:52:05.468 3069-3069/? W/System.err: at android.app.ActivityThread.-wrap11(ActivityThread.java)
03-22 14:52:05.468 3069-3069/? W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
03-22 14:52:05.468 3069-3069/? W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
03-22 14:52:05.468 3069-3069/? W/System.err: at android.os.Looper.loop(Looper.java:148)
03-22 14:52:05.468 3069-3069/? W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5417)
03-22 14:52:05.469 3069-3069/? W/System.err: at java.lang.reflect.Method.invoke(Native Method)
03-22 14:52:05.469 3069-3069/? W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
03-22 14:52:05.469 3069-3069/? W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
03-22 14:52:05.469 3069-3069/? W/System.err: Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
03-22 14:52:05.469 3069-3069/? W/System.err: at libcore.io.Posix.open(Native Method)
03-22 14:52:05.469 3069-3069/? W/System.err: at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
03-22 14:52:05.469 3069-3069/? W/System.err: at libcore.io.IoBridge.open(IoBridge.java:438)
03-22 14:52:05.469 3069-3069/? W/System.err: ... 17 more
03-22 14:52:05.584 3069-3083/? W/EGL_emulation: eglSurfaceAttrib not implemented
03-22 14:52:05.584 3069-3083/? W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xabe633a0, error=EGL_SUCCESS
03-22 14:52:06.188 1286-1305/? I/ActivityManager: Displayed com.example.jorgealberto.handleintents/.MainActivity: +754ms
答案 0 :(得分:0)
首先,openFileInput()
指向应用程序自己internal storage的部分。因此,第一个应用的openFileInput()
指向与第二个应用的openFileInput()
不同的位置。
其次,应用程序的内部存储中的文件对于该应用程序是私有的。您的第二个应用无权从第一个应用访问该文件。
相反,您需要使用some sort of ContentProvider
, such as FileProvider
,以便将文件提供给第三方应用。这包含在the documentation。