gpt4 book ai didi

java - Android Studio 应用程序 ImageView 拍照时不显示图像

转载 作者:行者123 更新时间:2023-12-01 10:02:18 25 4
gpt4 key购买 nike

我制作了一个使用手机摄像头拍摄照片并将其显示在 ImageView 中的应用程序,但它没有显示。

这是我的java代码。

package com.example.gonalo.photoactivitie;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import java.io.File;

public class MainActivity extends Activity {
Button button;
ImageView imageView;
static final int CAM_REQUEST = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
imageView = (ImageView) findViewById(R.id.image_view);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent camera_intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file = getFile();
camera_intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
startActivityForResult(camera_intent, CAM_REQUEST);


}
});
}

private File getFile() {

File folder = new File("SdCard/Camera");

if(!folder.exists()) {
folder.mkdir();
}

File image_file = new File(folder, "cam_image.lpg");

return image_file;
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
String path = "SdCard/Camera/cam_image.jpg";
imageView.setImageDrawable(Drawable.createFromPath(path));
}

这是我的 xml.file 代码

    <Button
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="Capture Image"
android:id="@+id/button"
android:layout_gravity="center_horizontal"
android:layout_below="@+id/image_view"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp" />
<ImageView
android:layout_width="350dp"
android:layout_height="300dp"
android:id="@+id/image_view"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"/>

这是我的manifest.file代码

  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

所以我认为这是因为我在java中说在拍照时创建一个新路径,但它没有这样做,我这么说是因为我的logcat。

04-18 22:54:03.940 1508-1508/? E/Zygote: MountEmulatedStorage()
04-18 22:54:03.940 1508-1508/? E/Zygote: v2
04-18 22:54:03.940 1508-1508/? I/libpersona: KNOX_SDCARD checking this for 10259
04-18 22:54:03.940 1508-1508/? I/libpersona: KNOX_SDCARD not a persona
04-18 22:54:03.940 1508-1508/? I/SELinux: Function: selinux_compare_spd_ram , priority [2] , priority version is VE=SEPF_SM-A500FU_5.0.2-1_0039
04-18 22:54:03.950 1508-1508/? E/SELinux: [DEBUG] get_category: variable seinfo: default sensitivity: NULL, cateogry: NULL
04-18 22:54:03.950 1508-1508/? I/art: Late-enabling -Xcheck:jni
04-18 22:54:03.990 1508-1508/? D/TimaKeyStoreProvider: TimaSignature is unavailable
04-18 22:54:03.990 1508-1508/? D/ActivityThread: Added TimaKeyStore provider
04-18 22:54:04.130 1508-1508/com.example.gonalo.photoactivitie D/PhoneWindow: *FMB* installDecor mIsFloating : false
04-18 22:54:04.130 1508-1508/com.example.gonalo.photoactivitie D/PhoneWindow: *FMB* installDecor flags : -2139029248
04-18 22:54:04.550 1508-1589/com.example.gonalo.photoactivitie D/OpenGLRenderer: Render dirty regions requested: true
04-18 22:54:04.570 1508-1508/com.example.gonalo.photoactivitie D/PhoneWindow: *FMB* isFloatingMenuEnabled mFloatingMenuBtn : null
04-18 22:54:04.570 1508-1508/com.example.gonalo.photoactivitie D/PhoneWindow: *FMB* isFloatingMenuEnabled return false
04-18 22:54:04.680 1508-1508/com.example.gonalo.photoactivitie D/SRIB_DCS: log_dcs ThreadedRenderer::initialize entered!
04-18 22:54:04.690 1508-1589/com.example.gonalo.photoactivitie I/Adreno-EGL: <qeglDrvAPI_eglInitialize:379>: EGL 1.4 QUALCOMM build: SKARAJGA_AU_LINUX_ANDROID_LA.BR.1.1.2_RB1.05.00.02.031.018+PATCH[ES]_msm8916_32_refs/tags/AU_LINUX_ANDROID_LA.BR.1.1.2_RB1.05.00.02.031.018__release_ENGG (I856e09677e)
OpenGL ES Shader Compiler Version: E031.25.03.02
Build Date: 04/06/15 Mon
Local Branch:
Remote Branch: refs/tags/AU_LINUX_ANDROID_LA.BR.1.1.2_RB1.05.00.02.031.018
Local Patches: 112c106f3772623daa7b4181c6cf23491044ead1 Revert "Disable ASTC on A405"
58a118cb818fdc906095a49a90977c15f9d3b223 Remove ASTC
04-18 22:54:04.700 1508-1589/com.example.gonalo.photoactivitie I/OpenGLRenderer: Initialized EGL, version 1.4
04-18 22:54:04.730 1508-1589/com.example.gonalo.photoactivitie D/OpenGLRenderer: Get maximum texture size. GL_MAX_TEXTURE_SIZE is 4096
04-18 22:54:04.730 1508-1589/com.example.gonalo.photoactivitie D/OpenGLRenderer: Enabling debug mode 0
04-18 22:54:04.810 1508-1508/com.example.gonalo.photoactivitie I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@104c84c4 time:444778388
04-18 22:54:06.010 1508-1508/com.example.gonalo.photoactivitie D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
04-18 22:54:06.230 1508-1508/com.example.gonalo.photoactivitie V/ActivityThread: updateVisibility : ActivityRecord{33fd7c3a token=android.os.BinderProxy@104c84c4 {com.example.gonalo.photoactivitie/com.example.gonalo.photoactivitie.MainActivity}} show : false
04-18 22:54:06.260 1508-1589/com.example.gonalo.photoactivitie D/OpenGLRenderer: endAllStagingAnimators on 0xb805b7a8 (RippleDrawable) with handle 0xb8062168
04-18 22:54:13.870 1508-1508/com.example.gonalo.photoactivitie E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: SdCard/Camera/cam_image.jpg: open failed: ENOENT (No such file or directory)
04-18 22:54:13.970 1508-1508/com.example.gonalo.photoactivitie D/SRIB_DCS: log_dcs ThreadedRenderer::initialize entered!
04-18 22:54:14.020 1508-1508/com.example.gonalo.photoactivitie I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@104c84c4 time:444787597

感谢所有帮助。

最佳答案

您没有提供正确的路径来创建可绘制对象。使用正确的路径,如下所示 -

imageView.setImageDrawable(Drawable.createFromPath(getFile().getAbsolutePath()));

此外,请更改您的 getFile(),因为您没有提供要创建的文件的正确路径 -

 private File getFile() {


File folder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "My_folder");

if(!folder.exists()) {
folder.mkdir();
}

File image_file = new File(folder, "cam_image.lpg");

return image_file;
}

关于java - Android Studio 应用程序 ImageView 拍照时不显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36705059/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com