Reputation: 1478
This sounds like an easy task, but I can't upload a bitmap, I took with camera, into my app. Well, technically I can, but nothing get's displayed.
I use this code for taking pictures on onClick
:
count++;
file = dir+count+".jpg";
File newfile = new File(file);
try {
newfile.createNewFile();
} catch (IOException e) {}
Uri outputFileUri = Uri.fromFile(newfile);
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(cameraIntent, TAKE_PHOTO_CODE);
Where
dir
- final String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
And this code for image loading:
String location = new String(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Pictures/" + count + ".jpg");
Bitmap myBitmap = BitmapFactory.decodeFile(location);
Bitmap mutableBitmap = myBitmap.copy(Bitmap.Config.ARGB_8888, true);
Canvas canvas = new Canvas(mutableBitmap);
wv2.draw(canvas);
tstImage.setImageBitmap(mutableBitmap);
text.setText(Float.toString(myBitmap.getHeight()) + " " + Float.toString(myBitmap.getWidth()) + " px");
Where
count
- Integer that represents name of the image
wv2
- custom WebView in which I draw canvas.
I do not think that these lines are relevant, but doesn't hurt to post additional info - you never know.
Canvas canvas = new Canvas(mutableBitmap);
wv2.draw(canvas);
This line displays text with dimensions of the image. I use it to check whether I load the bitmap.
text.setText(Float.toString(myBitmap.getHeight()) + " " + Float.toString(myBitmap.getWidth()) + " px");
When I run this code image does not get displayed, but text shows correct dimensions, which should mean that I do load the bitmap. What am I doing wrong? Where is the problem?
[UPDATE]
I think I'm making progress. Now I get outOfMemory problem and the app crashes.
String path = Environment.getExternalStorageDirectory()+ "/Pictures/" + count + ".jpg";
File imgFile = new File(path);
if(imgFile.exists()){
Bitmap mBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
tstImage.setImageBitmap(mBitmap);
} else {
Log.d("Ciaren", "File doesn't exist");
}
Upvotes: 0
Views: 210
Reputation: 21
OOM indicates that the image is way too big for memory to handle. There are two solutions here:
Either force your way through with largeHeap="true"
in the manifest, which is okay, but can lead to problems in the future.
Or you can change the options of bitmap loading, so it takes less memory at a time. Thus the image will take more time to load, but remove the possibility of the crash. Plus, the time difference is barely noticable.
Upvotes: 1
Reputation: 1069
Instead of using bitmap image get the uri or path and use picasso to load that image into imageView
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != Activity.RESULT_OK) {
return;
}
switch(requestcode){
case MEDIA_TYPE_IMAGE:
Uri selectedImageUri = data.getData();
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), seletedImageUri);
Picasso.with(getContext()).load(selectedImageUri).fit().into(imgDp);
break;
}
}
Upvotes: 0