我正在尝试将从图库加载的图像保存到手机内存(本地路径)。有人可以指导我吗?
这就是我从图库中获取图像的方式。
ImageView profilePicture;
private Uri imageUri;
String picturePath;
@Override
public void onCreate(Bundle savedInstanceState)
{
profilePicture = (ImageView) findViewById(R.id.profile_picture);
profilePicture.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
switch (arg1.getAction()) {
case MotionEvent.ACTION_DOWN: {
break;
}
case MotionEvent.ACTION_UP:{
uploadImage();
break;
}
}
return true;
}
});
}
上传图片()
Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, 1);
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 0:
if (resultCode == Activity.RESULT_OK) {
Uri selectedImage = imageUri;
getContentResolver().notifyChange(selectedImage, null);
ContentResolver cr = getContentResolver();
Bitmap bitmap;
try {
bitmap = android.provider.MediaStore.Images.Media
.getBitmap(cr, selectedImage);
profilePicture.setImageBitmap(bitmap);
} catch (Exception e) {
Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT)
.show();
Log.e("Camera", e.toString());
}
}
case 1:
if (resultCode == Activity.RESULT_OK && null != data) {
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]);
picturePath = cursor.getString(columnIndex);
cursor.close();
profilePicture.setBackgroundColor(Color.TRANSPARENT);
profilePicture.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
}
*注:案例 0 用于使用手机摄像头拍摄图像。
我可以在我的 imageview 上显示它,但我需要将它存储在手机的内存中,所以每次我打开应用程序时,我都可以将之前上传的图像加载到图像视图中。然后如果用户想再次上传。之前保存的文件将被覆盖。我不想使用 sqlite 将图像存储为 blob,因为我将只为我的整个应用程序上传一张图像。我想将它存储在本地文件路径中,例如 myappname/images/image.png。有任何想法吗?谢谢!