从图库中选取图像花费的时间太长
Pick image from gallery takes too long
在我的应用程序中,用户可以单击图像按钮,然后从图库中选择图像,然后将该图像设置为图像按钮。但问题是,对于大小为 5MB 或更大的图像,从画廊返回到我的 activity 需要很长时间,并且屏幕会变黑几秒钟。我对选中的图片不做任何操作,只想获取用户选中图片的路径
在AsyncTask中好像无法进行图片选取。那么我们如何处理大图像的图像选择过程。
当我从 logcat 的图库中挑选一张大图片时,我收到了
The application may be doing too much work on its main thread.
可能无法在 AsyncTask
内进行图像拾取。但是从图像 file/Uri 中解码位图肯定可以在 AsyncTask
.
中完成
此外,由于您在 ImageButton
中使用图像,因此您可能不需要使用全尺寸图像。使用 Options#inSampleSize
减小解码位图的大小。
正如 Nabin 回答的那样,您可以在 AsyncTask
中进行图像处理,我为您创建了一个小示例:
public class MainActivity extends AppCompatActivity {
public static final int PICK_IMAGE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE);
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PICK_IMAGE) {
Uri imagePath = data.getData();
new LoadImageDataTask(imagePath).execute();
}
}
private class LoadImageDataTask extends AsyncTask<Void, Void, Bitmap> {
private Uri imagePath;
LoadImageDataTask(Uri imagePath) {
this.imagePath = imagePath;
}
@Override
protected Bitmap doInBackground(Void... params) {
try {
InputStream imageStream = getContentResolver().openInputStream(imagePath);
return BitmapFactory.decodeStream(imageStream);
} catch (FileNotFoundException e) {
Toast.makeText(MainActivity.this, "The file " + imagePath + " does not exists",
Toast.LENGTH_SHORT).show();
}
return null;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
Toast.makeText(MainActivity.this, "I got the image data, with size: " +
Formatter.formatFileSize(MainActivity.this, bitmap.getByteCount()),
Toast.LENGTH_SHORT).show();
}
}
}
在后台工作,防止您的应用冻结。我什至可以非常快地选择 60MB 以上大小的图像。
在我的应用程序中,用户可以单击图像按钮,然后从图库中选择图像,然后将该图像设置为图像按钮。但问题是,对于大小为 5MB 或更大的图像,从画廊返回到我的 activity 需要很长时间,并且屏幕会变黑几秒钟。我对选中的图片不做任何操作,只想获取用户选中图片的路径
在AsyncTask中好像无法进行图片选取。那么我们如何处理大图像的图像选择过程。
当我从 logcat 的图库中挑选一张大图片时,我收到了
The application may be doing too much work on its main thread.
可能无法在 AsyncTask
内进行图像拾取。但是从图像 file/Uri 中解码位图肯定可以在 AsyncTask
.
此外,由于您在 ImageButton
中使用图像,因此您可能不需要使用全尺寸图像。使用 Options#inSampleSize
减小解码位图的大小。
正如 Nabin 回答的那样,您可以在 AsyncTask
中进行图像处理,我为您创建了一个小示例:
public class MainActivity extends AppCompatActivity {
public static final int PICK_IMAGE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE);
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PICK_IMAGE) {
Uri imagePath = data.getData();
new LoadImageDataTask(imagePath).execute();
}
}
private class LoadImageDataTask extends AsyncTask<Void, Void, Bitmap> {
private Uri imagePath;
LoadImageDataTask(Uri imagePath) {
this.imagePath = imagePath;
}
@Override
protected Bitmap doInBackground(Void... params) {
try {
InputStream imageStream = getContentResolver().openInputStream(imagePath);
return BitmapFactory.decodeStream(imageStream);
} catch (FileNotFoundException e) {
Toast.makeText(MainActivity.this, "The file " + imagePath + " does not exists",
Toast.LENGTH_SHORT).show();
}
return null;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
Toast.makeText(MainActivity.this, "I got the image data, with size: " +
Formatter.formatFileSize(MainActivity.this, bitmap.getByteCount()),
Toast.LENGTH_SHORT).show();
}
}
}
在后台工作,防止您的应用冻结。我什至可以非常快地选择 60MB 以上大小的图像。