これを機能させようとして、私はややイライラしています。誰かがこれで私を助けてくれませんか。私はこれが初めてで、Gallery/Image_capture から画像を取得して、常に ImageView に表示することだけが必要です。私は非常に多くのpplと行ったり来たりしており、diffの回答を得ています。
これは、editpic という ImageView を持つアクティビティです。ユーザーがクリックすると、ギャラリーまたはカメラから選択するよう求めるダイアログが開きます。
public int GET_CAM_IMG=2;
public int GET_GAL_IMG=1;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.profile);
profile();
private void profile() {
editpic = (ImageView)findViewById(R.id.editpic);
button1.setOnClickListener(new OnClickListener() {
public void onClick(View v){
CharSequence[] names = { "From Gallery", "From Camera" };
new AlertDialog.Builder(context)
.setTitle("Select an option for updating your Profile Picture")
.setItems(names, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int pos) {
// TODO Auto-generated method stub
if (pos == 0) {
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, GET_GAL_IMG);
} else {
Intent i = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, GET_CAM_IMG);
}
}
})
.setNegativeButton(android.R.string.cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
}
}).create().show();
}});
ユーザーがクリックすると、ケースとスイッチが通過します
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
switch (requestCode) {
case 2://Camera
if (resultCode == -1) {
String encodedImageString = null;
Uri selectedImage = intent.getData();
String selectedImagepath = getPath(selectedImage);
editpic.setImageURI(selectedImage);
Bitmap bmp_image = BitmapFactory.decodeFile(filePath);
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = context.getContentResolver().query(
selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
if (bmp_image.compress(Bitmap.CompressFormat.JPEG, 50, baos)) {
byte[] image = baos.toByteArray();
encodedImageString = Base64.encodeToString(image,
Base64.DEFAULT);
} else {
System.out.println("Compreesion returned false");
Log.d("Compress", "Compreesion returned false");
}
break;
case 3://Selecting from Gallery
if (resultCode == -1) {
String encodedImageString = null;
Bitmap bmp_image = null;
Bundle extras = intent.getExtras();
bmp_image = (Bitmap) extras.get("data");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
if (bmp_image.compress(Bitmap.CompressFormat.JPEG, 50, baos)) {
byte[] image = baos.toByteArray();
encodedImageString = Base64.encodeToString(image,
Base64.DEFAULT);
} else {
System.out.println("Compression returned false");
Log.d("Compress", "Compression returned false");
}
}
break;
}
}
イメージパスを取得してそれを使用できるようにメソッドを作成しようとしましたが、それを機能させることができませんでした
SharedPreferences userprofile = getSharedPreferences(filename,0);
SharedPreferences.Editor editor = userprofile.edit();
editor.putString("Imagepath",selectedImagepath);
editor.commit();
写真を撮った後、画像は Imageview に表示されますが、消えます。これはプロフィール写真なので、そこに置いておきたいです。電話帳の連絡先の写真のようなものだと思われます。