2016-02-04 66 views
0

我正在開發一款旨在將智能手機變成顯微鏡的項目(也有一些圖像處理);我已決定在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

回答

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

該值是錯誤的〜1.5十億的Android設備。

更一般地,從不硬編碼路徑。總是使用某種方法來派生根區來寫入。您似乎希望寫信給external storage。在這種情況下,請使用getExternalFilesDir()(在Context上)或Environment上的方法獲取要寫入的根位置。

+0

非常感謝您回答我的問題。 –

相關問題