Android 模拟器不显示存储的图像

Android emulator does not show image stored

我正在从事一个旨在将智能phone 变成显微镜的项目(还有一点图像处理);我决定在 Android Studio 中构建我的程序 android phones.

我在网上找到了一些关于如何访问相机、捕获图像并将其存储在内存中的教程。出于某种原因,用于 运行 程序显示的模拟器仅捕获图像,但不将其存储在内存中。他们是模拟器的问题吗?如何将我正在编写的程序传输到我的 Android Google Nexus 5 phone?

这里有一些 XML 代码:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.user.smartphonemicroscope">
<uses-feature android:name="android.hardware.camera2"></uses-feature> <!-- Acess camera2 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".Microscope">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>
</manifest>

这里有一些 Java:

package com.example.user.smartphonemicroscope;
import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.preference.PreferenceManager;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.hardware.camera2.CameraDevice;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import java.io.File;

public class Microscope 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_microscope);
    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_app");

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

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

    return image_file;
}

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

我还有几张界面图片。

Emulator Camera Accessed, after pressing capture button
Image Captured, but will not save

File folder = new File("sdcard/camera_app");

该值在约 15 亿 Android 台设备上是错误的。

更一般地说,从不对路径进行硬编码。始终使用某种方法导出要写入的根位置。您似乎希望写信给 external storage。在这种情况下,使用 getExternalFilesDir()(在 Context 上)或 Environment 上的方法来获取要写入的根位置。