简体   繁体   English

发送图像(位图/文件)意图Android

[英]Send Image (Bitmap/File) Intent Android

I am developing 2 applications one for send image and the other for receive it. 我正在开发2个应用程序,一个用于发送图像,另一个用于接收图像。 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 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. 该错误始终存在,当我尝试从文件发送uri时,我也会遇到同样的错误。 (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

First, openFileInput() points to an app's own portion of internal storage . 首先, openFileInput()指向应用程序自身的内部存储部分。 Hence, openFileInput() of the first app points to a different location than does openFileInput() of the second app. 因此,第一个应用程序的openFileInput()指向与第二个应用程序的openFileInput()不同的位置。

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. 相反,您需要使用某种ContentProvider ,例如FileProvider ,以使文件可用于第三方应用程序。 This is covered in the documentation . 文档中对此进行了介绍

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM