2016-02-25 53 views
8

我有一個RecyclerView適配器,它具有許多不同的ViewHolders。其中一個ViewHolders包含一個ImageView,它需要能夠拍照,調整大小,然後顯示它。對於模塊性,我希望ViewHolder是獨立的:它不是父級活動應該處理與照片拍攝和顯示過程有關的所有事情。此外,文件路徑是不變的(它永遠不會改變)。實際上,它是/storage/emulated/0/com.company.app/myst/cat.jpg。因此,這裏是我實現ImageView的onClick方法。如果方向改變,相機不會覆蓋舊圖像

@Override 
public void onClick(View v) { 
    final FragmentManager fm = ((MyActivity) getContext()).getSupportFragmentManager(); 
    Fragment auxiliary = new Fragment() { 
     @Override 
     public void onActivityResult(int requestCode, int resultCode, Intent data) { 
      resizeResaveAndDisplayPhoto(); 
      super.onActivityResult(requestCode, resultCode, data); 
      fm.beginTransaction().remove(this).commit(); 
     } 
    }; 
    fm.beginTransaction().add(auxiliary, "FRAGMENT_TAG").commit(); 
    fm.executePendingTransactions(); 

    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
    if (null != takePictureIntent.resolveActivity(view.getContext().getPackageManager())) { 
     ((MyActivity)view.getContext()).setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
     takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile)); 
     auxFragment.startActivityForResult(takePictureIntent, Constants.REQUEST_CODE_PHOTO); 
    } 
} 

resizeResaveAndDisplayPhoto把它叫做執行下面的AsyncTask

public static class ResizeThenLoadImageTask extends AsyncTask<String, Void, Bitmap> { 

    private final WeakReference<ImageView> imageViewWeakReference; 
    private final WeakReference<File> fileWeakReference; 
    private final WeakReference<Context> weakContext; 
    private final int reqHeight; 
    private final int reqWidth; 

    public ResizeThenLoadImageTask(Context context, ImageView imageView, File file, int reqHeight, int reqWidth) { 
     weakContext = new WeakReference<Context>(context); 
     imageViewWeakReference = new WeakReference<>(imageView); 
     fileWeakReference = new WeakReference(file); 
     this.reqHeight = reqHeight; 
     this.reqWidth = reqWidth; 
    } 

    @Override 
    public Bitmap doInBackground(String... params) { 
     File file = fileWeakReference.get(); 
     Bitmap bitmap = null; 
     if (null != file) { 
      bitmap = ImageUtils.reduceImageSize(file, reqHeight, reqWidth); 
      ImageUtils.saveBitmapToGivenFile(bitmap, file); 
     } 
     return bitmap; 
    } 

    @Override 
    public void onPostExecute(Bitmap bitmap) { 
     if (null != imageViewWeakReference && null != fileWeakReference) { 
      ImageView imageView = imageViewWeakReference.get(); 
      File file = fileWeakReference.get(); 
      if (null != imageView) { 
       if (null != bitmap) { 
        imageView.setImageBitmap(bitmap); 
       } 
       else { 
        imageView.setImageResource(R.drawable.photo); 
       } 
       imageView.postDelayed(new Runnable() { 
        @Override 
        public void run() { 
         if (null != weakContext.get()) { 
          ((MyActivity) weakContext.get()).setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED); 
         } 
        } 
       }, 10000); 
      } 
     } 
    } 
} 

您可能注意到,我拍攝照片之前鎖定的方位和顯示照片後解鎖10秒。那個技巧是我的疑難解答的一部分。所以情況就是這樣。上述系統工作得很好。在以下情況下發生問題

  • 說我已經在ImageView中有一張照片,但想要替換它。
  • 所以我點擊ImageView拍攝一張新照片。
  • 如果我旋轉設備拍攝新照片,然後在舊照片返回之前短暫返回新的照片顯示。
  • 所以我鎖定方向,看看發生了什麼。這是我發現的。
  • 只要我鎖定方向,新照片就會顯示。一旦方向解鎖(10秒),舊照片就會返回。
  • 如果我離開活動和退貨,舊照片仍在顯示。
  • 如果我完全關閉應用程序然後返回,那麼我會看到新照片。
+0

所以我用了三分之一的要點來問這個問題。請給出有意義的答案。 –

+0

您是否僅面向此方向或僅始終顯示默認圖像? –

+0

向我們展示何時首次創建「活動」或在方向更改時重新創建的代碼,其中您將位圖從文件加載到「ImageView」中。 –

回答

0

當設備旋轉時,會重新創建運行活動以及連接到它的asyncTask,可能會導致泄漏。

您可能需要在服務內部調用asyncTask,而不是在活動中,以便將asyncTask附加到服務的生命週期。

+0

你的答案忽略了一點。它並不能解釋爲什麼文件內容會回滾到前一張照片。請閱讀整個問題。 –

+0

以前的語言太混亂了。所以我編輯了文本。 –

+0

我不確定我的答案是否能解決您的問題,是嗎?如果是這樣,我會編輯我的答案,試圖解釋爲什麼文件內容會回滾到前一張照片,如果沒有,它沒有意義嘗試解釋它。 – cherif