在我的应用程序中,它是一个简单的图像显示应用程序,我可以在其中添加来自外部存储的多个图像
添加图像时,添加图像花费的时间太长,有时还会发生 $dm = $this->getDocumentManager();
$dm->createQueryBuilder('DnDReactivePandoraBundle:Property')
->remove()
->field('listingId')->notIn($ids)
->field('country')->equals($country)
->getQuery()
->execute();
的错误。
如何解决此问题。请帮忙!
这是一些代码。...
我的 ImageCollection 活动
OutOfMemory
}
这是适配器类...
public class ImageCollection extends AppCompatActivity {
public static final String IMAGE_SHARED_PREFS = "com.example.animproject_IMAGE_SHARED_PREFS";
public static final String IMAGE_DATA_KEY = "com.example.animproject_IMAGE_DATA_KEY";
private static final int REQUEST_CODE = 1;
RecyclerView recyclerView;
ImageCollectionAdapter adapter;
List<ImageModelClass> imageList;
FloatingActionButton fab;
String imageStringConvert;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_collection);
loadAlbumData();
recyclerView = findViewById(R.id.imageCollectionRecyclerView);
recyclerView.hasFixedSize();
adapter = new ImageCollectionAdapter(imageList, ImageLoader.getInstance(), imageStringConvert);
StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(2, GridLayoutManager.VERTICAL);
recyclerView.setLayoutManager(staggeredGridLayoutManager);
recyclerView.setAdapter(adapter);
fab = findViewById(R.id.fabButton);
fab.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
@Override
public void onClick(View view) {
Intent gallery = new Intent(Intent.ACTION_PICK);
gallery.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
gallery.setType("image/*");
startActivityForResult(gallery, REQUEST_CODE);
}
});
}
public static String encodeTobase64(Bitmap bitmap) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] b = baos.toByteArray();
return Base64.encodeToString(b, Base64.DEFAULT);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
assert data != null;
ClipData clipData = data.getClipData();
if (clipData != null) {
for (int i = 0; i < clipData.getItemCount(); i++) {
Uri imageUri = clipData.getItemAt(i).getUri();
InputStream inputStream;
try {
inputStream = getContentResolver().openInputStream(imageUri);
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
ImageModelClass imageModelClass = new ImageModelClass();
imageStringConvert = encodeTobase64(bitmap);
imageModelClass.setImage(imageStringConvert);
imageList.add(imageModelClass);
adapter.notifyDataSetChanged();
saveGalleryData();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
} else {
Uri imageUri = data.getData();
InputStream inputStream = null;
try {
assert imageUri != null;
inputStream = getContentResolver().openInputStream(imageUri);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
imageStringConvert = encodeTobase64(bitmap);
ImageModelClass imageModelClass = new ImageModelClass();
imageModelClass.setImage(imageStringConvert);
imageList.add(imageModelClass);
adapter.notifyDataSetChanged();
saveGalleryData();
}
}
}
private void saveGalleryData() {
SharedPreferences preferences = getSharedPreferences(IMAGE_SHARED_PREFS, MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
Gson gson = new Gson();
String json = gson.toJson(imageList);
editor.putString(IMAGE_DATA_KEY, json);
editor.apply();
}
private void loadAlbumData() {
SharedPreferences preferences = getSharedPreferences(IMAGE_SHARED_PREFS, MODE_PRIVATE);
Gson gson = new Gson();
Type type = new TypeToken<ArrayList<ImageModelClass>>() {
}.getType();
String data = preferences.getString(IMAGE_DATA_KEY, null);
imageList = gson.fromJson(data, type);
if (imageList == null) {
imageList = new ArrayList<>();
}
}
}
答案 0 :(得分:1)
尝试在线程中加载图像
new Thread(new Runnable() {
@Override
public void run() {
//load Image in onActivityResult
}
}).start();
您还可以使用Base64编码降低图像质量
bitmap.compress(Bitmap.CompressFormat.PNG,/*quality of image maximum is 100*/ 100, baos);
答案 1 :(得分:1)
使用
放置您的onBindViewHolderHandler mainHandler = new Handler(Looper.getMainLooper());
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
ImageModelClass currentImage = list.get(position);
new Thread(new Runnable() {
@Override
public void run() {
byte[] decodedByte = Base64.decode(currentImage.getImage(), 0);
Bitmap bitmap = BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length);
mainHandler.post(new Runnable() {
@Override
public void run() {
holder.imageView.setImageBitmap(bitmap);
}
}
}
}).start();
}