我遇到一个问题,我必须从图库中取出图像并在压缩后将其保存到存储卡中。我可以从图库中选择图像甚至保存它。但不知怎的,似乎有一些编码错误,因为我无法打开图像。这是代码:
public class MainActivity extends Activity {
ImageView image;
Button save, add;
String fileName;
final Context context = this;
Bitmap bitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
save = (Button)findViewById(R.id.buttonSave);
add = (Button)findViewById(R.id.buttonAdd);
image = (ImageView)findViewById(R.id.imageView);
add.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 0);
}
});
save.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
LayoutInflater li = LayoutInflater.from(context);
View promptsView = li.inflate(R.layout.rename_dialog, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
// set prompts.xml to alertdialog builder
alertDialogBuilder.setView(promptsView);
final EditText userInput = (EditText) promptsView
.findViewById(R.id.editRename);
alertDialogBuilder
.setTitle("Save Image")
.setMessage("Enter name")
.setNegativeButton("Save", new DialogInterface.OnClickListener() {
//
@Override
public void onClick(DialogInterface dialog, int which) {
fileName = userInput.getText().toString();
Log.d("Name" , fileName);
saveFile(null, fileName);
Toast.makeText(getApplicationContext(), "Image saved", Toast.LENGTH_SHORT).show();
}
})
.setPositiveButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.cancel();
Toast.makeText(getApplicationContext(), "Image save cancelled", Toast.LENGTH_SHORT).show();
}
});
AlertDialog build = alertDialogBuilder.create();
build.show();
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK){
Uri targetUri = data.getData();
try {
bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(targetUri));
image.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private void saveFile(Bitmap images, String fileName) {
BitmapDrawable drawable = (BitmapDrawable) image.getDrawable();
bitmap = drawable.getBitmap();
File sdCard = Environment.getExternalStorageDirectory();
File pic = new File(sdCard , fileName + ".jpeg");
boolean success = false;
FileOutputStream outStream;
try {
outStream = new FileOutputStream(pic);
//bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
/* 100 to keep full quality of the image */
outStream.flush();
outStream.close();
success = true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (success) {
Toast.makeText(getApplicationContext(), "Image saved with success",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(),
"Error during image saving", Toast.LENGTH_LONG).show();
}
}
}