首先请原谅我的英语不好。
我想从相机/图库中选取图像,以便在布局中显示并保存。关闭程序并重新打开程序后,我想检索它。
到目前为止,我到了这里:
public class MainActivity extends Activity {
ImageView img_logo;
protected static final int CAMERA_REQUEST = 0;
protected static final int GALLERY_PICTURE = 1;
private Intent pictureActionIntent = null;
Bitmap bitmap;
String selectedImagePath;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img_logo= (ImageView) findViewById(R.id.imageView1);
img_logo.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
startDialog();
}
});
}
protected void startDialog() {
AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(this);
myAlertDialog.setTitle("Upload Pictures Option");
myAlertDialog.setMessage("How do you want to set your picture?");
myAlertDialog.setPositiveButton("Gallery",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
pictureActionIntent = new Intent(
Intent.ACTION_GET_CONTENT, null);
pictureActionIntent.setType("image/*");
pictureActionIntent.putExtra("return-data", true);
startActivityForResult(pictureActionIntent,
GALLERY_PICTURE);
}
});
myAlertDialog.setNegativeButton("Camera",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
pictureActionIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(pictureActionIntent,
CAMERA_REQUEST);
}
});
myAlertDialog.show();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == GALLERY_PICTURE) {
if (resultCode == RESULT_OK) {
if (data != null) {
// our BitmapDrawable for the thumbnail
BitmapDrawable bmpDrawable = null;
// try to retrieve the image using the data from the intent
Cursor cursor = getContentResolver().query(data.getData(),
null, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
int idx = cursor.getColumnIndex(ImageColumns.DATA);
String fileSrc = cursor.getString(idx);
bitmap = BitmapFactory.decodeFile(fileSrc); // load
// preview
// image
bitmap = Bitmap.createScaledBitmap(bitmap,
100, 100, false);
// bmpDrawable = new BitmapDrawable(bitmapPreview);
img_logo.setImageBitmap(bitmap);
} else {
bmpDrawable = new BitmapDrawable(getResources(), data
.getData().getPath());
img_logo.setImageDrawable(bmpDrawable);
}
} else {
Toast.makeText(getApplicationContext(), "Cancelled",
Toast.LENGTH_SHORT).show();
}
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(getApplicationContext(), "Cancelled",
Toast.LENGTH_SHORT).show();
}
} else if (requestCode == CAMERA_REQUEST) {
if (resultCode == RESULT_OK) {
if (data.hasExtra("data")) {
// retrieve the bitmap from the intent
bitmap = (Bitmap) data.getExtras().get("data");
Cursor cursor = getContentResolver()
.query(Media.EXTERNAL_CONTENT_URI,
new String[] {
Media.DATA,
Media.DATE_ADDED,
MediaStore.Images.ImageColumns.ORIENTATION },
Media.DATE_ADDED, null, "date_added ASC");
if (cursor != null && cursor.moveToFirst()) {
do {
Uri uri = Uri.parse(cursor.getString(cursor
.getColumnIndex(Media.DATA)));
selectedImagePath = uri.toString();
} while (cursor.moveToNext());
cursor.close();
}
Log.e("path of the image from camera ====> ",
selectedImagePath);
bitmap = Bitmap.createScaledBitmap(bitmap, 100,
100, false);
// update the image view with the bitmap
img_logo.setImageBitmap(bitmap);
} else if (data.getExtras() == null) {
Toast.makeText(getApplicationContext(),
"No extras to retrieve!", Toast.LENGTH_SHORT)
.show();
BitmapDrawable thumbnail = new BitmapDrawable(
getResources(), data.getData().getPath());
// update the image view with the newly created drawable
img_logo.setImageDrawable(thumbnail);
}
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(getApplicationContext(), "Cancelled",
Toast.LENGTH_SHORT).show();
}
}
}
}
它在选择或接受时工作正常,但它不能保存。 我知道我应该在共享首选项中保存图像路径,然后再次恢复它,但是如何?
如果你可以只编辑我的代码并做我想要的那将是非常棒的。 如果不只是给我一些信息。 非常感谢你。