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

问题描述:

我正在开发一款旨在将智能手机变成显微镜的项目(也有一些图像处理);我已决定在Android Studio中为Android手机构建我的程序。Android模拟器不显示存储的图像

在线查看我找到了一些关于如何访问摄像头,捕捉图像并将其存储在内存中的教程。出于某种原因,用于运行该程序的仿真器仅显示捕捉图像,但不会将其存储在内存中。他们是模拟器的问题吗?我将如何将我正在写给我的Android Google Nexus 5手机的这个程序转移?

这里是一些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

+0

要在您的物理硬件上进行测试,只需将手机插入PC并启用USB调试,然后在android studio中选择您的硬件即可。还请查看CommonWares的答案。 – basic

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

该值是错误的〜1.5十亿的Android设备。

更一般地,从不硬编码路径。总是使用某种方法来派生根区来写入。您似乎希望写信给external storage。在这种情况下,请使用getExternalFilesDir()(在Context上)或Environment上的方法获取要写入的根位置。

+0

非常感谢您回答我的问题。 –