我有一個圖像採集方法,它可以讓用戶從他們的畫廊選擇一個圖像。Android的性能一旦圖片選擇是可怕的
一旦圖像被選中,應用程序變得非常可怕,頁面上有3個文本框,當你點擊它們需要大約3秒鐘時間纔會出現鍵盤,並且它出現在laggy階段。
logcat的警告:
12-03 19:03:35.536 10842-10842/.com. I/Choreographer﹕ Skipped 58 frames! The
application may be doing too much work on its main thread.
所以,我試圖把圖片選擇一個新的線程,但有太多的方法來單獨封裝。
對於這樣一個簡單的任務,低性能的常見問題/解決方案是什麼?
代碼:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_submit);
imageView = (ImageView) findViewById(R.id.submitImageButton);
button = (ImageView) findViewById(R.id.submitImageButton);
}
public void pickPhoto(View view) {
new Thread(new Runnable() {
public void run() {
// launch the photo picker
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,
"Select Picture"), SELECT_PICTURE);
}
}).start();
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK) {
try {
Bitmap bitmap = getPath(data.getData());
Log.i("Bitmap", "Bmp: " + data.getData());
imageView.setImageBitmap(bitmap);
}catch (Exception e){
Log.e("Error", "Error with setting the image. See stack trace.");
e.printStackTrace();
}
}
}
private Bitmap getPath(Uri uri) {
button.setEnabled(false);
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
cursor.moveToFirst();
String filePath = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
cursor.close();
// Convert file path into bitmap image using below line.
Bitmap bitmap = BitmapFactory.decodeFile(filePath);
filePathForUpload = filePath;
try {
ExifInterface exif = new ExifInterface(filePath);
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);
bitmap = rotateBitmap(bitmap, orientation);
}catch (Exception e){
Log.d("Error", "error with bitmap!");
e.printStackTrace();
}
return bitmap;
}
public static Bitmap rotateBitmap(Bitmap bitmap, int orientation) {
Matrix matrix = new Matrix();
switch (orientation) {
case ExifInterface.ORIENTATION_NORMAL:
return bitmap;
case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
matrix.setScale(-1, 1);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
matrix.setRotate(180);
break;
case ExifInterface.ORIENTATION_FLIP_VERTICAL:
matrix.setRotate(180);
matrix.postScale(-1, 1);
break;
case ExifInterface.ORIENTATION_TRANSPOSE:
matrix.setRotate(90);
matrix.postScale(-1, 1);
break;
case ExifInterface.ORIENTATION_ROTATE_90:
matrix.setRotate(90);
break;
case ExifInterface.ORIENTATION_TRANSVERSE:
matrix.setRotate(-90);
matrix.postScale(-1, 1);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
matrix.setRotate(-90);
break;
default:
return bitmap;
}
Bitmap bmRotated = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
bitmap.recycle();
return bmRotated;
}
'pickPhoto'中的線程是毫無意義的,你可以刪除它(活動不會在後臺線程中啓動,您調用的方法只是發佈啓動它的請求)。另一方面,'getPath' .. – zapl 2014-12-03 19:22:48
你需要將你的內容解析器/遊標的東西推到一個單獨的線程上。就像@zapl所說的那樣,儘管在pickPhoto中沒有任何目的。 – zgc7009 2014-12-03 19:22:48