2013-07-30 60 views
6

以下是我的相關問題code snippet在相機拍攝照片後,其中重定向到設備的cropping app。我通常會遵循這個example。但是在從相機拍攝的照片和在裁剪應用程序中提供的照片之間的階段/時刻,它顯示持續加載並使我的應用程序非常反應。如果這種情況至少發生一次,每次打開應用程序,它都會顯示加載。從相機拍攝時裁剪圖像問題

one scenario - >當我沒有移動我的設備時(即只有當沒有屏幕方向發生時)纔會發生此問題。有人可以建議我避免這個問題嗎?

public void shotFromCamera(View view) 
{ 
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
     intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageCaptureUri); 

       try { 
        intent.putExtra("return-data", true); 

        startActivityForResult(intent, PICK_FROM_CAMERA); 
       } catch (ActivityNotFoundException e) { 
        e.printStackTrace(); 
       } 
} 

protected void onActivityResult(int requestCode, int resultCode,Intent data) 
    { 
     super.onActivityResult(requestCode, resultCode, data); 

     if(resultCode==RESULT_OK && requestCode == PICK_FROM_CAMERA){ 

      doCrop(); 
       } else if(resultCode == RESULT_OK && requestCode == CROP_FROM_CAMERA) 
     { 
      Bundle extras = data.getExtras(); 

      if (extras != null) {    
       Bitmap photoBitmap = extras.getParcelable("data"); 

       imageView.setImageBitmap(photoBitmap); 
       if(mImageCaptureUri != null) 
       { 
       File f = new File(mImageCaptureUri.getPath()); 
       if (f.exists()) f.delete(); 
       } 

       mImageCaptureUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory()+ "/MyApp", 
          "avatar" + String.valueOf(System.currentTimeMillis()) + ".jpg")); 
       File file = new File(mImageCaptureUri.getPath()); 

        selectedImagePath = mImageCaptureUri.getPath(); 
        Log.d("pic1 ","pic to be created: "+selectedImagePath); 
        OutputStream fOut = null; 

        try { 
         fOut = new FileOutputStream(file); 
         photoBitmap.compress(Bitmap.CompressFormat.JPEG, 85, fOut); 
         fOut.flush(); 
         fOut.close(); 
        } catch (FileNotFoundException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } catch(IOException e) 
        { 
         e.printStackTrace(); 
        }  

      } 


     } 
} 

    private void doCrop() { 
      final ArrayList<CropOption> cropOptions = new ArrayList<CropOption>(); 
      Intent intent = new Intent("com.android.camera.action.CROP"); 
      intent.setType("image/*"); 

      List<ResolveInfo> list = getPackageManager().queryIntentActivities(intent, 0); 

      int size = list.size(); 

      if (size == 0) {    
       Toast.makeText(this, "Can not find image crop app", Toast.LENGTH_SHORT).show(); 

       return; 
      } else { 
       intent.setData(imageCaptureUri); 

       intent.putExtra("outputX", 200); 
       intent.putExtra("outputY", 200); 
       intent.putExtra("aspectX", 1); 
       intent.putExtra("aspectY", 1); 
       intent.putExtra("scale", true); 
       intent.putExtra("return-data", true); 

       if (size == 1) { 
        Intent i  = new Intent(intent); 
        ResolveInfo res = list.get(0); 

        i.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name)); 

        startActivityForResult(i, CROP_FROM_CAMERA); 
       } 
    } 

爲了澄清我的問題,我已經運行該示例中的源代碼,因爲它是相同的問題發生。我已經運行了其他幾個重定向到裁剪應用的例子。在上述解釋階段改變方向時會發生同樣的問題。

P.S:如果任何人有一個工作實例,即使在方向變化時也可以工作,我會很樂意接受它作爲答案。

回答

0

檢查imageCaptureUri的價值!=行intent.setData(imageCaptureUri);前空在doCrop()

編輯

首先保存在文件中的相機的結果,並將該文件用於裁剪應用

intent.setData(Uri.fromFile(new File(path))); 
+0

是的,它繞過,但它不能解決我的問題。即使在這種情況下,您是否還有其他建議和任何工作場景? – Kanth

+0

拍照使用相機請參閱http://developer.android.com/training/camera/photobasics.html –

1

你可以跟這個一起去 -

  1. 從指定的URI,即使用

    photo = android.provider.MediaStore.Images.Media.getBitmap(cr, Uri.fromFile(imageCaptureUri));

  2. 使用Bitmap.CompressFormat.PNG全尺寸的圖象得到位圖,同時壓縮使用圖像

  3. 創建縮放位圖

    photo= Bitmap.createScaledBitmap(background, YourWidth, YourHeight, true);

  4. 這是您的縮放(裁剪)圖像。

+1

感謝您的回答:)我可以做到這一點。但我想讓用戶可以自己裁剪設備。 – Kanth