发送图像(位图/文件)意图Android(Send Image (Bitmap/File) Intent Android)

系统教程 行业动态 更新时间:2024-06-14 16:57:18
发送图像(位图/文件)意图Android(Send Image (Bitmap/File) Intent Android)

我正在开发2个应用程序,一个用于发送图像,另一个用于接收它。 我在发送图像文件或位图时遇到同样的问题。 我已经在这里阅读了很多问答,但没有任何帮助。

第一个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

I am developing 2 applications one for send image and the other for receive it. I have the same problem sending the image file or the Bitmap. I already read lot of Q&A here but nothing help.

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>

1st App class ( using 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(); }

2nd 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>

2nd App class

@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(); } } }

The error always have, when i try to send uri from file i take same error. (I think is because i can't open file in 2nd application but i don't know how open it, i tried to save and open in so many places but nothing help)

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

最满意答案

首先, openFileInput()指向应用程序自己的内部存储部分。 因此,第一个应用程序的openFileInput()指向与第二个应用程序的openFileInput()不同的位置。

其次,应用程序的内部存储中的文件对于该应用程序是私有的。 您的第二个应用无权从第一个应用访问该文件。

相反,您需要使用某种ContentProvider (例如FileProvider )来使文件可供第三方应用程序使用。 这在文档中有所介绍。

First, openFileInput() points to an app's own portion of internal storage. Hence, openFileInput() of the first app points to a different location than does openFileInput() of the second app.

Second, files in internal storage for an app are private for that app. Your second app has no rights to access the file from the first app.

Instead, you need to use some sort of ContentProvider, such as FileProvider, to make the files available to third-party apps. This is covered in the documentation.

更多推荐

本文发布于:2023-04-12 20:32:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/aa94b6a7ea2b011ab96cc4dd85611efd.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:位图   意图   图像   文件   Android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!