Nou*_*vay 8 filesystems camera android android-asynctask
我有一个带有许多不同ViewHolders的RecyclerView适配器.其中一个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);
}
}
Run Code Online (Sandbox Code Playgroud)
当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);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
您可能会注意到我在拍照前锁定了方向,并在显示照片10秒后将其解锁.这个技巧是我的故障排除的一部分.所以情况就是这样.上述系统工作得很好.在下列情况下会出现问题
当设备旋转时,正在运行的活动以及附加的 asyncTask 都会被重新创建,这可能会导致泄漏。
您可能需要在服务内部而不是活动中调用 asyncTask,以便 asyncTask 将附加到服务的生命周期。
归档时间: |
|
查看次数: |
80 次 |
最近记录: |