问题
I am picking the picture from gallery or taking it with the camera. If I get the picture into my imageView, and click the confirm button, how can I then save that picture? Do I have to use saveState()? Please post some comments. Thanks.
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != RESULT_OK) return;
switch (requestCode)
{
case PICK_FROM_CAMERA:
Bitmap selectedImage = (Bitmap) data.getExtras().get("data");
selectedImage = Bitmap.createScaledBitmap(selectedImage, 80, 80, false);
mImageView.setImageBitmap(selectedImage);
break;
case PICK_FROM_GALLERY:
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
System.out.println("Image Path : " + selectedImagePath);
mImageView.setImageURI(selectedImageUri);
break;
}
}
private void saveState()
{
String name = (String) nameEdit.getText().toString();
String category = (String) categoryEdit.getText().toString();
String expired_date = (String) expired_Date_Btn.getText().toString();
ImageView image = (ImageView) mImageView.setImageURI(); //how to edit?
if(mRowId == null)
{
long id = mDbHelper.insertItem(category, name, expired_date);
if(id>0)
{
mRowId = id;
}
}
else
{
mDbHelper.updateItem(mRowId, category, name, expired_date);
}
}
//How can I save image after clicking button?
confirmButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v){
setResult(RESULT_OK);
finish();
}
});
回答1:
You can save the image of all View (not only ImageView) following these steps:
1.Get the bitmap of your view:
public Bitmap loadBitmapFromView(View v) {
Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(),
Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.draw(c);
v.invalidate();
return b;
}
2.Save it on your SD card file (or wherever you want):
protected String saveBitmap(Bitmap bm, String name) throws Exception {
String tempFilePath = Environment.getExternalStorageDirectory() + "/"
+ getPackageName() + "/" + name + ".jpg";
File tempFile = new File(tempFilePath);
if (!tempFile.exists()) {
if (!tempFile.getParentFile().exists()) {
tempFile.getParentFile().mkdirs();
}
}
tempFile.delete();
tempFile.createNewFile();
int quality = 100;
FileOutputStream fileOutputStream = new FileOutputStream(tempFile);
BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);
bm.compress(CompressFormat.JPEG, quality, bos);
bos.flush();
bos.close();
bm.recycle();
return tempFilePath;
}
These code take from one of my project, but I think they are easy to understand and re-use. Hope it will help you.
回答2:
I'm not sure how to do this from the gallery or why you would want to since the image is already saved to the phone if it is in the gallery. You should be able to re-write the file using the file's URI though. If you are taking the image with the camera, you can see that you have a Bitmap of the image. It should be relatively easy to save it using the following snippet of code:
outStream = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.PNG, 100, outStream);
outStream.flush();
outStream.close();
You will want the permission
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
For more information, try following this example (it is where I found the code snippets). They are uploading their image but the same concepts should apply. http://android-er.blogspot.com/2010/07/save-file-to-sd-card.html
Hope this helps!
来源:https://stackoverflow.com/questions/8320094/how-to-save-an-image-from-an-imageview-after-taking-a-picture