ちょっと友人をお読みください。ボタンをクリック - カメラを開いて画像をキャプチャする
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, fileName);
mCapturedImageURI = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent cameraIntent = new Intent("android.media.action.IMAGE_CAPTURE");
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
2)。ボタンをクリックすると選択した画像のギャラリーが開きます
Intent galleryintent = new Intent(Intent.ACTION_GET_CONTENT);
galleryintent.setType("image/*");
startActivityForResult(galleryintent, IMAGE_PICK);
3)。静的変数
private static final int CAMERA_REQUEST = 0;
private static final int IMAGE_PICK = 1;
4)。 onActivityResult
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
switch(requestCode)
{
case CAMERA_REQUEST:
if(resultCode == RESULT_OK)
{
String[] projection = { MediaStore.Images.Media.DATA};
Cursor cursor = managedQuery(mCapturedImageURI, projection, null, null, null);
int column_index_data = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String capturedImageFilePath = cursor.getString(column_index_data);
Log.d("photos*******"," in camera take int "+capturedImageFilePath);
Bitmap photo_camera = BitmapFactory.decodeFile(capturedImageFilePath, options);
if(data != null)
{
img_1.setImageBitmap(photo_camera);
prefsEditor.putString(Global.PHOTO_1,capturedImageFilePath);
}
}
case IMAGE_PICK:
if(resultCode == RESULT_OK)
{
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
// Bitmap photo = BitmapFactory.decodeFile(filePath);
Bitmap photo_gallery = BitmapFactory.decodeFile(filePath,options);
img_1.setImageBitmap(photo_gallery);
prefsEditor.putString(Global.PHOTO_1, filePath);
}
}
prefsEditor.commit();
}
5)。 onDestroy() 設定したすべてのビットマップを破棄する必要があります。
@Override
public void onDestroy()
{
super.onDestroy();
if(photo_camera != null)
{
photo_camera.recycle();
}
if(photo_gallery != null)
{
photo_gallery.recycle();
}
}
6)。 sharedPrefrencesからデータを取得するときに、文字列をビットマップに変換してからImageViewでビットマップを設定する必要があります。 たとえば、Bitmap bit1 = BitmapFactory.decodeFile(strimg1); を設定してから、imageView.setImageBitmap
を実行してください。これは間違いなく助けになります。 [1] [1] を[こちらをクリック]:http://stackoverflow.com/questions/17268519/how-to-store-bitmap-object-in-sharedpreferences-in-android私が使用 –