0
我试图将从图库加载的图像保存到手机内存(本地路径)。任何人都可以引导我进入这个?将图像保存到从图库加载的手机
这是我从库中获取图像。
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;
}
});
}
uploadImage()
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这样的本地文件路径中。有任何想法吗?谢谢!
我会尝试。但是,请您指出保存图像中的哪一部分是我引用当前图像的那一行?如你看到的。我从图库中获得的图像直接存储在imageview中。这条线。 profilePicture.setImageBitmap(BitmapFactory.decodeFile(picturePath)); bitmap.com是否具有我拥有的图像?所以我应该将BitmapFactory.decodeFile(picturePath)存储为一个位图。 BM。然后在压缩前使用这个:bm.compress? – ljpv14
这是你的位图:BitmapFactory.decodeFile(picturePath));所以尝试设置:BitmapFactory.decodeFile(picturePath))。compress(Bitmap.CompressFormat.PNG,100,out); –
我将它存储到一个位图变量,使其更加有组织。 +1美好而简单的回答。正是我需要的。没有其他不必要的代码和解决方法。开门见山。谢谢! – ljpv14