私と私の友人は、このインテント カメラ アプリを作成して、写真を撮り、それをフォルダーに保存し、それを imageView に表示し、写真のハッシュ値を作成しようとしました。ここでの問題は、最初の撮影では問題なく動作しますが、2 回目の撮影を続行しようとするとエラーが発生することです。コードに見落としている問題がないか確認してください。
ここにコードがあります
public class CameraApp extends Activity {
private static final int IMAGE_CAPTURE = 0;
private ImageButton startBtn;
private ImageButton gallery;
private Uri imageUri;
private ImageView imageView;
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
Date now = new Date();
String fileName = formatter.format(now);
final Context context = this;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera_app);
imageView = (ImageView)findViewById(R.id.img);
startBtn = (ImageButton) findViewById(R.id.startBtn);
gallery = (ImageButton)findViewById(R.id.gallery);
startBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startCamera();
}
});
gallery.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory(),"camera_app")),"image/*");
startActivity(i);
}
});
}
public void startCamera() {
File photo = null;
File folder= new File(Environment.getExternalStorageDirectory(),"camera_app");
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
if(folder.exists()){
if (android.os.Environment.getExternalStorageState().equals( android.os.Environment.MEDIA_MOUNTED)) {
photo = new File(android.os.Environment.getExternalStorageDirectory(), "camera_app/DF_" + fileName + "_PIC.jpg");
}
}
else{
folder.mkdirs();
photo = new File(android.os.Environment.getExternalStorageDirectory(), "camera_app/DF_" + fileName + "_PIC.jpg");
}
/*else {
photo = new File(getCacheDir(), "camera_app/FILE_NAME.jpg");
} */
if (photo != null) {
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
imageUri = Uri.fromFile(photo);
startActivityForResult(intent, IMAGE_CAPTURE);
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == IMAGE_CAPTURE) {
if (resultCode == RESULT_OK){
//try {
Uri selectedImage = imageUri;
ContentResolver cr = getContentResolver();
//InputStream inputStream = cr.openInputStream(imageUri);
Bitmap bitmap;
try {
InputStream inputStream = cr.openInputStream(imageUri);
bitmap = android.provider.MediaStore.Images.Media.getBitmap(cr, selectedImage);
imageView.setImageBitmap(bitmap);
java.security.MessageDigest md = MessageDigest.getInstance("MD5");
byte[] buffer = new byte[1024];
int read;
do {
read = inputStream.read(buffer);
if (read > 0)
md.update(buffer, 0, read);
} while (read != -1);
inputStream.close();
byte[] digest = md.digest();
String hexString = "";
for (int i = 0; i < digest.length; i++) {
hexString += Integer.toString((digest[i] & 0xff)
+ 0x100, 16).substring(1);
}
final EditText input = new EditText(this);
input.setText("DF_" + fileName + "_PIC_");
new AlertDialog.Builder(this)
.setTitle("Rename")
.setMessage("<MD5> "+ hexString)
.setView(input)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String value = input.getText().toString();
String value1 = value + ".jpg";
final File F=new File(android.os.Environment.getExternalStorageDirectory(), "camera_app/DF_" + fileName + "_PIC.jpg");
File newfile=new File(F.getParent(),value1);
F.renameTo(newfile);
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Do nothing.
}
})
.show();
} catch (Exception e) {
Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT).show();
Log.e("Camera", e.toString());
}
} else {
imageUri = null;
imageView.setImageBitmap(null);
}
}
}
}
xml コード
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:id="@+id/img"
android:layout_width="fill_parent"
android:layout_height="350dip"
android:layout_gravity="center"
android:src="@drawable/camera"
android:scaleType="fitStart"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageButton
android:orientation="horizontal"
android:id="@+id/startBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="@null"
android:src="@drawable/img_button"/>
<ImageButton
android:id="@+id/gallery"
android:layout_width="430dip"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="@null"
android:src="@drawable/gallery_icon"/>
</LinearLayout>
</LinearLayout>
マニフェスト
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
logcat エラー
08-27 05:58:02.505: E/vold(556): Error opening switch name path '/sys/class/switch/test2' (No such file or directory)
08-27 05:58:02.505: E/vold(556): Error bootstrapping switch '/sys/class/switch/test2' (m)
08-27 05:58:02.505: E/vold(556): Error opening switch name path '/sys/class/switch/test' (No such file or directory)
08-27 05:58:02.505: E/vold(556): Error bootstrapping switch '/sys/class/switch/test' (m)
08-27 05:58:02.626: E/flash_image(564): can't find recovery partition
08-27 05:58:16.086: E/MemoryHeapBase(590): error opening /dev/pmem: No such file or directory
08-27 05:58:16.086: E/SurfaceFlinger(590): Couldn't open /sys/power/wait_for_fb_sleep or /sys/power/wait_for_fb_wake
08-27 05:58:16.186: E/GLLogger(590): couldn't load <libhgl.so> library (Cannot find library)
08-27 05:58:16.276: E/GLLogger(590): couldn't load <libhgl.so> library (Cannot find library)
08-27 05:58:19.757: E/BatteryService(590): Could not open '/sys/class/power_supply/usb/online'
08-27 05:58:19.757: E/BatteryService(590): Could not open '/sys/class/power_supply/battery/batt_vol'
08-27 05:58:19.757: E/BatteryService(590): Could not open '/sys/class/power_supply/battery/batt_temp'
08-27 05:58:20.355: E/EventHub(590): could not get driver version for /dev/input/mouse0, Not a typewriter
08-27 05:58:20.526: E/EventHub(590): could not get driver version for /dev/input/mice, Not a typewriter
08-27 05:58:20.885: E/System(590): Failure starting core service
08-27 05:58:20.885: E/System(590): java.lang.SecurityException
08-27 05:58:20.885: E/System(590): at android.os.BinderProxy.transact(Native Method)
08-27 05:58:20.885: E/System(590): at android.os.ServiceManagerProxy.addService(ServiceManagerNative.java:146)
08-27 05:58:20.885: E/System(590): at android.os.ServiceManager.addService(ServiceManager.java:72)
08-27 05:58:20.885: E/System(590): at com.android.server.ServerThread.run(SystemServer.java:163)
08-27 05:58:20.895: E/AndroidRuntime(590): Crash logging skipped, no checkin service
08-27 05:58:22.086: E/LockPatternKeyguardView(590): Failed to bind to GLS while checking for account
08-27 05:58:25.155: E/ApplicationContext(590): Couldn't create directory for SharedPreferences file shared_prefs/wallpaper-hints.xml
08-27 05:58:25.890: E/ActivityThread(632): Failed to find provider info for android.server.checkin
08-27 05:58:26.681: E/ActivityThread(632): Failed to find provider info for android.server.checkin
08-27 05:58:26.782: E/ActivityThread(632): Failed to find provider info for android.server.checkin
08-27 05:58:34.680: E/MediaPlayer(561): Unable to to create media player
08-27 05:58:34.680: E/CameraService(561): Failed to load CameraService sounds.
08-27 05:58:34.710: E/MediaPlayer(561): Unable to to create media player
08-27 05:58:34.720: E/CameraService(561): Failed to load CameraService sounds.
08-27 05:58:36.660: E/Camera(738): _getParameters: picture-format=jpeg;picture-size=213x350;preview-format=yuv422sp;preview-frame-rate=15;preview-size=176x144
08-27 05:58:36.660: E/Camera(738): setParameters()
08-27 05:58:37.901: E/Camera(738): setParameters()
08-27 05:58:46.891: E/MediaPlayer(561): Unable to to create media player
08-27 05:58:46.891: E/CameraService(561): Failed to load CameraService sounds.
08-27 05:58:46.891: E/MediaPlayer(561): Unable to to create media player
08-27 05:58:46.891: E/CameraService(561): Failed to load CameraService sounds.
08-27 05:58:47.821: E/Camera(738): _getParameters: picture-format=jpeg;picture-size=213x350;preview-format=yuv422sp;preview-frame-rate=15;preview-size=176x144
08-27 05:58:47.840: E/Camera(738): setParameters()
08-27 05:58:48.910: E/Camera(738): setParameters()
08-27 05:59:10.970: E/MediaPlayer(561): Unable to to create media player
08-27 05:59:10.970: E/CameraService(561): Failed to load CameraService sounds.
08-27 05:59:11.000: E/MediaPlayer(561): Unable to to create media player
08-27 05:59:11.000: E/CameraService(561): Failed to load CameraService sounds.
08-27 05:59:11.990: E/Camera(738): _getParameters: picture-format=jpeg;picture-size=213x350;preview-format=yuv422sp;preview-frame-rate=15;preview-size=176x144
08-27 05:59:11.990: E/Camera(738): setParameters()
08-27 05:59:14.530: E/Camera(738): setParameters()
: E/(): Device disconnected