2014-12-03 2 views
0

나는 이미지 갤러리에서 이미지를 선택할 수있는 이미지 피킹 방법을 사용하고 있습니다.안드로이드 성능을 선택하면 이미지가 끔찍합니다.

일단 이미지가 선택되면 앱이 크게 뒤떨어져 페이지에 3 개의 텍스트 상자가 있습니다. 클릭하면 키보드가 올라 오는데 약 3 초가 걸리며 느린 단계가됩니다.

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; 
    } 
+1

'pickPhoto'의 스레드는 무의미하며 제거 할 수 있습니다 (액티비티는 백그라운드 스레드에서 실행되지 않으며, 호출하는 메소드는이를 시작하라는 요청을 게시합니다). 'getPath'를 사용합니다 .. – zapl

+0

콘텐츠 리졸버/커서를 다른 스레드로 밀어 넣어야합니다. @ 자플처럼, pickPhoto에서 그것을 가지고 아무 목적도. – zgc7009

답변

1
당신은 (즉, getPath (...)과 별도의 스레드에서 비트 맵 생성을 넣어해야

rotateBitmap (...), (pickPhoto을 (사진을 선택하지 의도 보기보기).

또한 새 비트 맵을 만들어 비트 맵을 회전하는 대신 ImageView에 비트 맵을 설정하고 View.setRotation (float x)을 사용하는 것이 좋을 것입니다. 이미지를 회전 할 때마다 새 비트 맵을 사용하십시오.

+0

위대한 - 나는 그것이 실이 무거웠다는 것을 깨닫지 못했다. 나는 그 기회를 잡을거야. –

관련 문제