我解決了這個問題,這裏是用的(android:select image from gallery then crop that and show in an imageview後在計算器(感謝Dhaval帕特爾的幫助和atifali)裁切圖像,並將其設置爲我的ImageView的背景Android的
我的活動類更新的代碼:
public class MainActivity extends Activity {
private static int RESULT_LOAD_IMAGE = 1;
String filePath;
ImageView bitmapView;
private final int RESULT_CROP = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FrameLayout wal = (FrameLayout) findViewById(R.id.fm);
wal.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try{
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}catch(Exception e){
e.printStackTrace();
}}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode ==RESULT_OK && null != data) {
Uri picUri = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(picUri,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
filePath = cursor.getString(columnIndex);
cursor.close();
doCrop(filePath);
if (requestCode == RESULT_CROP) {
if(resultCode == Activity.RESULT_OK){
Bundle extras = data.getExtras();
Bitmap selectedBitmap = extras.getParcelable("data");
// Set The Bitmap Data To ImageView
bitmapview.setImageBitmap(selectedBitmap);
bitmapview.setScaleType(ScaleType.FIT_XY);
}
}
}
}
private void doCrop(String picPath) {
try {
Intent cropIntent = new Intent("com.android.camera.action.CROP");
File f = new File(picPath);
Uri contentUri = Uri.fromFile(f);
cropIntent.setDataAndType(contentUri, "image/*");
cropIntent.putExtra("crop", "true");
// indicate aspect of desired crop
cropIntent.putExtra("aspectX", 1);
cropIntent.putExtra("aspectY", 1);
// indicate output X and Y
cropIntent.putExtra("outputX", 280);
cropIntent.putExtra("outputY", 280);
// retrieve data on return
cropIntent.putExtra("return-data", true);
// start the activity - we handle returning in onActivityResult
startActivityForResult(cropIntent, RESULT_CROP);
}
catch (ActivityNotFoundException anfe) {
String errorMessage = "your device doesn't support the crop action!";
Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
toast.show();
}
}
}
請指定您遇到的問題。 – atifali
嗨Atifail,其實圖像越來越裁剪,但imageview的背景反映與原始centercrop圖像instead.I的意思是我從圖片庫中選擇圖像,然後下一個裁剪它,當我點擊保存此裁剪圖像不反映原始圖像從畫廊只坐在imageview。 – Adithya