无法在android studio中正确上传和显示图像

时间:2016-02-18 14:50:50

标签: java android image android-studio

我正在尝试使用此tutorial创建一个从SD卡上传图像并在我的Android屏幕上打印的界面。

然而,当我从我的应用程序上传图像时,它不会上传并打印在我的屏幕上。我在/sdcard/image.jpg中有图像但是当我在代码中检查图像的路径时,它给出/ document / image:11。位图为空。任何人都可以帮助找到代码中的错误并更正它吗?

这是我的代码:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != RESULT_OK) {

            return;
        }

        Bitmap bitmap   = null;
        String path     = "";

        if (requestCode == PICK_FROM_FILE) {
            mImageCaptureUri = data.getData();
            path = getRealPathFromURI(mImageCaptureUri); //from Gallery

            if (path == null)
                path = mImageCaptureUri.getPath(); //from File Manager

            if (path != null)
                bitmap  = BitmapFactory.decodeFile(path);
        } else {
            path    = mImageCaptureUri.getPath();
            bitmap  = BitmapFactory.decodeFile(path);
        }
        if(bitmap==null) Toast.makeText(this, path, Toast.LENGTH_LONG).show();
        Log.d("SOMETHING", path);

        mImageView.setImageBitmap(bitmap);

    }

这是我的logcat:

02-18 20:03:51.430 25924-25924/? I/art: Not late-enabling -Xcheck:jni (already on)
02-18 20:03:51.580 25924-25939/? D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
02-18 20:03:51.810 25924-25931/? I/art: Debugger is no longer active
02-18 20:03:52.060 25924-25924/? D/Atlas: Validating map...
02-18 20:03:52.170 25924-25939/? I/OpenGLRenderer: Initialized EGL, version 1.4
02-18 20:03:52.730 25924-25939/? D/OpenGLRenderer: Enabling debug mode 0
W/EGL_emulation: eglSurfaceAttrib not implemented
W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x7ffb1ffdff00, error=EGL_SUCCESS
I/Choreographer: Skipped 43 frames!  The application may be doing too much work on its main thread.
I/Choreographer: Skipped 82 frames!  The application may be doing too much work on its main thread.
W/EGL_emulation: eglSurfaceAttrib not implemented
W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x7ffb178eb580, error=EGL_SUCCESS
E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /document/image:11: open failed: ENOENT (No such file or directory)
W/CursorWrapperInner: Cursor finalized without prior close()
W/EGL_emulation: eglSurfaceAttrib not implemented
W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x7ffb179dd940, error=EGL_SUCCESS
W/EGL_emulation: eglSurfaceAttrib not implemented
W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x7ffb1ffdff00, error=EGL_SUCCESS
W/EGL_emulation: eglSurfaceAttrib not implemented
W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x7ffb178eb5c0, error=EGL_SUCCESS
W/EGL_emulation: eglSurfaceAttrib not implemented
W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x7ffb178ebcc0, error=EGL_SUCCESS
W/InputEventReceiver: Attempted to finish an input event but the input event receiver has already been disposed.
E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /document/image:11: open failed: ENOENT (No such file or directory)
W/EGL_emulation: eglSurfaceAttrib not implemented
W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x7ffb179dd940, error=EGL_SUCCESS
W/EGL_emulation: eglSurfaceAttrib not implemented
W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x7ffb1fcd3680, error=EGL_SUCCESS
I/Choreographer: Skipped 310 frames!  The application may be doing too much work on its main thread.

1 个答案:

答案 0 :(得分:0)

该教程中存在错误。

在定义SD卡的意图时,请替换

intent.setAction(Intent.ACTION_GET_CONTENT);

intent.setAction(Intent.ACTION_PICK);