[英]Not able to upload and show the image properly in android studio
I am trying to create an interface to upload image from sd card and print it on my android screen using this tutorial . 我正在尝试创建一个界面来使用本教程从SD卡上传图像并将其打印在android屏幕上。
However, when I upload the image from my app, it does not get uploaded and printed on my screen. 但是,当我从应用程序上传图像时,它不会被上传并打印在屏幕上。 I have the image in /sdcard/image.jpg but when I check the path of the image in my code, it gives /document/image:11. 我在/sdcard/image.jpg中有图像,但是当我在代码中检查图像的路径时,它给出了/ document / image:11。 The bitmap is null. 位图为空。 Can anyone help to find the error in the code and correct it? 谁能帮助您找到代码中的错误并予以纠正?
Here is my code: 这是我的代码:
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);
}
Here is my 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.
There's a mistake in that tutorial. 该教程有一个错误。
When defining the intent for sd card, replace 定义SD卡的意图时,请更换
intent.setAction(Intent.ACTION_GET_CONTENT);
with 与
intent.setAction(Intent.ACTION_PICK);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.