In my android application i need to take a picture. The first picture goes fine and uploads great to the server so it can be sent by email to the user. That's working fine. Yet when i want to upload an 2nd image it says Out Of Memory Exception. I don't know why, but somehow it does.
My logcat output can be found at this pastebin link: http://pastebin.com/uVduy3d9
My code for handling the image is as following:
First check if phone has a camera:
Camera cam = Camera.open();
if (cam != null) {
if (savedInstanceState != null
&& savedInstanceState.getBoolean("Layout")) {
setContentView(R.layout.registration_edit);
initializeAccountDetails((User) savedInstanceState
.getSerializable(EXTRA_MESSAGE));
inAccountDetails = true;
} else {
setContentView(R.layout.step_4);
((Button) findViewById(R.id.snap)).setOnClickListener(this);
((Button) findViewById(R.id.rotate)).setOnClickListener(this);
cam.stopPreview();
cam.release();
cam = null;
}
} else {
if (savedInstanceState != null
&& savedInstanceState.getBoolean("Layout")) {
setContentView(R.layout.registration_edit);
initializeAccountDetails((User) savedInstanceState
.getSerializable(EXTRA_MESSAGE));
inAccountDetails = true;
} else {
setContentView(R.layout.step_4b);
}
}
When clicking on the button Snap the following onClick event is fired:
#Override
public void onClick(View v) {
if (v.getId() == R.id.snap) {
File directory = new File(Environment.getExternalStorageDirectory()
+ "/BandenAnalyse/Images/");
if (directory.exists()) {
Intent i = new Intent("android.media.action.IMAGE_CAPTURE");
File f = new File(Environment.getExternalStorageDirectory(),
"/BandenAnalyse/Images/IMG_" + _timeStamp + ".jpg");
_mUri = Uri.fromFile(f);
i.putExtra(MediaStore.EXTRA_OUTPUT, _mUri);
startActivityForResult(i, TAKE_PICTURE);
} else {
directory.mkdir();
this.onClick(v);
}
} else {
if (_mPhoto != null) {
Matrix matrix = new Matrix();
matrix.postRotate(90);
_mPhoto = Bitmap.createBitmap(_mPhoto, 0, 0,
_mPhoto.getWidth(), _mPhoto.getHeight(), matrix, true);
((ImageView) findViewById(R.id.photo_holder))
.setImageBitmap(_mPhoto);
_mPhoto.recycle();
}
}
}
When the picture is taken the result method will be fired:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case TAKE_PICTURE:
if (resultCode == Activity.RESULT_OK) {
getContentResolver().notifyChange(_mUri, null);
ContentResolver cr = getContentResolver();
try {
_mPhoto = android.provider.MediaStore.Images.Media
.getBitmap(cr, _mUri);
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int scale = _mPhoto.getWidth() / width;
BitmapFactory.Options o = new BitmapFactory.Options();
o.inSampleSize = 8;
Debug.out(PATH_TO_PHOTO);
Bitmap temp = BitmapFactory.decodeFile(PATH_TO_PHOTO, o);
_mPhoto = Bitmap.createScaledBitmap(
temp,
_mPhoto.getWidth() / scale, _mPhoto.getHeight()
/ scale, false);
temp.recycle();
((ImageView) findViewById(R.id.photo_holder))
.setImageBitmap(_mPhoto);
} catch (Exception e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT)
.show();
}
}
}
}
The error should be in the last method, as when i'm in the cameramode and want to get back to my application the error occurs.
How to fix this error? Did i miss something?
EDIT:
Added Code in the function: OnActivityResult.
Created a temp object as one of the solutions said. Too bad this didn't help solving the error.
The error Out of memory Exception occurs at the line:
_mPhoto = android.provider.MediaStore.Images.Media.getBitmap(cr, _mUri);
You shouldn't create a bitmap and using it without keeping a reference on it since you have to release it for good memory management.
what you do :
_mPhoto = Bitmap.createScaledBitmap(
BitmapFactory.decodeFile(PATH_TO_PHOTO, o),
_mPhoto.getWidth() / scale, _mPhoto.getHeight()
/ scale, false);
is bad ! ;)
prefer :
Bitmap temp = BitmapFactory.decodeFile(PATH_TO_PHOTO, o);
_mPhoto = Bitmap.createScaledBitmap(
temp,
_mPhoto.getWidth() / scale, _mPhoto.getHeight()
/ scale, false);
temp.recycle(); //this call is the key ;)
Read your code with in mind : "every bitmap created has to be recycle or it will crash with OOM error at some point".
hope that helps !
you should read more about android Bitmaps and memory management for a complete understanding ;)
Related
I'm building an app that require method to take picture from in-app camera, but for some Android devices (old device or low ram), it's quite freeze when taking picture triggered. Is there any code i can modify or optimize to make user experience feels better?
//this function trigger to take picture (or screenshot) from user screen
private void captureImage() {
mPreview.setDrawingCacheEnabled(true);
final Bitmap[] drawingCache = {Bitmap.createBitmap(mPreview.getDrawingCache())};
mPreview.setDrawingCacheEnabled(false);
mCameraSource.takePicture(null, bytes -> {
int orientation = Exif.getOrientation(bytes);
Bitmap temp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
Bitmap picture = rotateImage(temp, orientation);
assert picture != null;
Bitmap overlay = Bitmap.createBitmap(mGraphicOverlay.getWidth(), mGraphicOverlay.getHeight(), picture.getConfig());
Canvas canvas = new Canvas(overlay);
Matrix matrix = new Matrix();
matrix.setScale((float) overlay.getWidth() / (float) picture.getWidth(), (float) overlay.getHeight() / (float) picture.getHeight());
// mirror by inverting scale and translating
matrix.preScale(-1, 1);
matrix.postTranslate(canvas.getWidth(), 0);
Paint paint = new Paint();
canvas.drawBitmap(picture, matrix, paint);
canvas.drawBitmap(drawingCache[0], 0, 0, paint);
//this function to save picture taken and put it on app storage cache
try {
String mainpath = getApplicationContext().getFilesDir().getPath() + separator + "e-Presensi" + separator;
File basePath = new File(mainpath);
if (!basePath.exists())
Log.d("CAPTURE_BASE_PATH", basePath.mkdirs() ? "Success" : "Failed");
//this function to get directory path of saved photo
String path = mainpath + "photo_" + getPhotoTime() + ".jpg";
String namafotoo = "photo_" + getPhotoTime() + ".jpg";
filePath = path;
namafoto = namafotoo;
SessionManager.createNamaFoto(namafoto);
File captureFile = new File(path);
boolean sucess = captureFile.createNewFile();
if (!captureFile.exists())
Log.d("CAPTURE_FILE_PATH", sucess ? "Success" : "Failed");
FileOutputStream stream = new FileOutputStream(captureFile);
overlay.compress(Bitmap.CompressFormat.WEBP, 60, stream);
stream.flush();
stream.close();
if (!picture.isRecycled()) {
picture.recycle();
}
if (drawingCache[0] != null && !drawingCache[0].isRecycled()) {
drawingCache[0].recycle();
drawingCache[0] = null;
}
mPreview.setDrawingCacheEnabled(false);
uploadPicture();
finish();
} catch (IOException e) {
e.printStackTrace();
}
});
}
Thanks for your help.
In general, I would advise you to step through your code and look at large memory resources you're generating on each line and consider setting those to null aggressively as you move throughout the method if you're done.
For example, you have a variable called temp which is size "Y" bytes that you appear to rotate and then never use temp after that. If picture is a rotated copy of temp then you have now used 2Y bytes of memory to keep temp and picture. I suspect if you simply set temp to null after creating picture, you might free up half that memory that your older/slower phones are going to badly need.
Take that same concept and follow through with the rest of your method to see if you can find other optimizations. Basically anywhere you're creating a copy of the image data you're not going to use, you should immediately set it to null so the garbage collector can throw it away aggressively.
Firt you need some variables:
byte[] byteArray_IMG;
String currentPhotoPath;
static final int REQUEST_TAKE_PHOTO = 1;
ImageView imageView; // maybe you need show the photo before send it
Then define the method to take the photo:
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
Toast.makeText(getApplicationContext(),Error takin the photo",Toast.LENGTH_LONG).show();
}
// Continue only if the File was successfully created
if (photoFile != null) {
path_img = photoFile.toString();
Uri photoURI = FileProvider.getUriForFile(this,"com.app.yournmamepackage.fileprovider" , photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
}
Create the method to create the file image
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
currentPhotoPath = image.getAbsolutePath();
return image;
}
Override the activity result to:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode,resultCode,data);
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
/* Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
imageView.setImageBitmap(imageBitmap);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
imageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byteArray_IMG = stream.toByteArray();*/
MediaScannerConnection.scanFile(this, new String[]{path_img}, null,
new MediaScannerConnection.OnScanCompletedListener() {
#Override
public void onScanCompleted(String path, Uri uri) {
Log.i("path",""+path);
}
});
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Bitmap imageBitmap = BitmapFactory.decodeFile(path_img);
imageBitmap.compress(Bitmap.CompressFormat.JPEG, 14, stream);
imageView.setImageBitmap(imageBitmap);
byteArray_IMG = stream.toByteArray();
}
}
Remember this is very important
imageBitmap.compress(Bitmap.CompressFormat.JPEG, 25, stream)
// 25 is photo quality 0-100
Then you can upload the picture usin an asynchronous process
Firstly Initialize these variables above onCreate() method in your activity/fragment
val FILE_NAME:String="photo.jpg" //give any name with.jpg extension
private var imageuri: Uri?=null
val REQUEST_IMAGE=111
Now open camera
val intent: Intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
photofile = getphotofile(FILE_NAME)
imageuri = activity?.let { it1 -> FileProvider.getUriForFile(it1, "//your package name here//.fileprovider", photofile) } //put your package name
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageuri)
startActivityForResult(int, REQUEST_IMAGE)
onActivityResult() method
if(requestCode==REQUEST_IMAGE && resultCode == Activity.RESULT_OK){
val progressdialog: ProgressDialog = ProgressDialog(activity)
progressdialog.setTitle("Sending Image....")
progressdialog.show() //start your progressdialog
val ref= FirebaseDatabase.getInstance().reference
val messagekey=ref.push().key //create a key to store image in firebase
var bmp: Bitmap?=null
try {
bmp=MediaStore.Images.Media.getBitmap(activity?.contentResolver,imageuri) //get image in bitmap
} catch (e: IOException) {
e.printStackTrace();
}
val baos= ByteArrayOutputStream()
bmp!!.compress(Bitmap.CompressFormat.JPEG,50,baos) //compress the quality of image to 50 from 100
val fileinBytes: ByteArray =baos.toByteArray()
val store: StorageReference = FirebaseStorage.getInstance().reference.child("Chat Images/") //create a child reference in firebase
val path=store.child("$messagekey.jpg") //store a message in above reference with the unique key created above with .jpg extension
val uploadTask: StorageTask<*>
uploadTask=path.putBytes(fileinBytes) //it will upload the image to firebase at given path
uploadTask.continueWithTask(Continuation<UploadTask.TaskSnapshot, Task<Uri>> { task ->
if (!task.isSuccessful) {
task.exception?.let {
throw it
}
Toast.makeText(activity, "Upload Failed", Toast.LENGTH_SHORT).show()
}
return#Continuation path.downloadUrl
}).addOnCompleteListener { task->
if(task.isSuccessful){
url=task.result.toString() //get the url of uploaded image
//Do what you want with the url of your image
progressdialog.dismiss()
Toast.makeText(activity, "Image Uploaded", Toast.LENGTH_SHORT).show()
}
}.addOnFailureListener { e->
progressdialog.dismiss()
Toast.makeText(activity, "Failed " + e.message, Toast.LENGTH_SHORT)
.show();
}
uploadTask.addOnProgressListener { task->
var progress:Double=(100.0*task.bytesTransferred)/task.totalByteCount
progressdialog.setMessage("Sending ${progress.toInt()}%") //this will show the progress in progress bar 0-100%
}
}
I am using the ZXing library for barcode scanning.
I want to scan a barcode using this library from an image (e.g., on the SD card) rather than from the camera.
How can I do this using the ZXing library?
I wanted to test it out before posting it so it took me a while, I'm also using ZXing right now so this comes handy for me as well:
First of course, read the image from the gallery (this can be in your activity):
Intent pickIntent = new Intent(Intent.ACTION_PICK);
pickIntent.setDataAndType( android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
startActivityForResult(pickIntent, 111);
After that, just get the image uri on the activity result and then ZXing will do the magic:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
//the case is because you might be handling multiple request codes here
case 111:
if(data == null || data.getData()==null) {
Log.e("TAG", "The uri is null, probably the user cancelled the image selection process using the back button.");
return;
}
Uri uri = data.getData();
try
{
InputStream inputStream = getContentResolver().openInputStream(uri);
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
if (bitmap == null)
{
Log.e("TAG", "uri is not a bitmap," + uri.toString());
return;
}
int width = bitmap.getWidth(), height = bitmap.getHeight();
int[] pixels = new int[width * height];
bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
bitmap.recycle();
bitmap = null;
RGBLuminanceSource source = new RGBLuminanceSource(width, height, pixels);
BinaryBitmap bBitmap = new BinaryBitmap(new HybridBinarizer(source));
MultiFormatReader reader = new MultiFormatReader();
try
{
Result result = reader.decode(bBitmap);
Toast.makeText(this, "The content of the QR image is: " + result.getText(), Toast.LENGTH_SHORT).show();
}
catch (NotFoundException e)
{
Log.e("TAG", "decode exception", e);
}
}
catch (FileNotFoundException e)
{
Log.e("TAG", "can not open file" + uri.toString(), e);
}
break;
}
}
I tested this and it works, cheers.
In our application, the user presses a button which opens the Android default camera activity.
Snippet on how we start the camera activity:
Uri uri = FileProvider.getUriForFile(this, FILE_PROVIDER, image);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(intent, 1);
The problem is if the Activity is in portrait mode, everything works fine, camera is in portrait mode, image gets saved in the correct orientation, etc.
In landscape mode though, the camera starts and after returning the activity is stuck in portrait mode and does not change by tilting the phone anymore.
Does anyone know a solution for this problem?
EDIT: code of onActivityResult(...)
EDIT2: resolved custom methods
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == CAMERA_ACTIVITY_CODE && resultCode == Activity.RESULT_OK) {
//returns Filehandler to image
File objektImage = getImageForObjekt(loadedObjekt);
correctImageRotation(objektImage);
//ImageView
this.imgObjekt.setImageDrawable(Drawable.createFromPath(objektImage.getAbsolutePath()));
}
}
This method corrects the image rotation - cameras on Samsung phones stored them in the wrong orientation which is corrected here:
public boolean correctImageRotation(File img) {
ExifInterface ei;
try {
ei = new ExifInterface(img.getAbsolutePath());
} catch (IOException e) {
e.printStackTrace();
return false;
}
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
Log.d(this._LOGKEY, getLogPraefix()+" Store the new Objekt image to: " + img.getName());
Bitmap bm = BitmapFactory.decodeFile(img.getAbsolutePath());
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
bm = rotateImage(bm, 90);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
bm = rotateImage(bm, 180);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
bm = rotateImage(bm, 270);
break;
}
bm = ImageHelper.squarify(bm);
bm = ImageHelper.compress(bm, ImageHelper.Size.large);
try {
FileOutputStream stream = new FileOutputStream(img);
bm.compress(Bitmap.CompressFormat.JPEG, 90, stream);
stream.flush();
stream.close();
return true;
} catch (FileNotFoundException e) {
Log.e(_LOGKEY, getLogPraefix()+": file not found.");
return false;
} catch (IOException e) {
Log.e(_LOGKEY, getLogPraefix()+": IO problem.");
return false;
}
}
This method cuts the image s.t. it is square-shaped afterwards:
public static Bitmap squarify(Bitmap srcBmp) {
Bitmap dstBmp;
if (srcBmp.getWidth() >= srcBmp.getHeight()){
dstBmp = Bitmap.createBitmap(
srcBmp,
srcBmp.getWidth()/2 - srcBmp.getHeight()/2,
0,
srcBmp.getHeight(),
srcBmp.getHeight()
);
}else{
dstBmp = Bitmap.createBitmap(
srcBmp,
0,
srcBmp.getHeight()/2 - srcBmp.getWidth()/2,
srcBmp.getWidth(),
srcBmp.getWidth()
);
}
return dstBmp;
}
And finally this method compresses the image in one of three possible sizes:
public static Bitmap compress(Bitmap srcBmp, Size size) {
int side;
switch (size) {
default:
case small:
side = 200;
break;
case medium:
side = 400;
break;
case large:
side = 800;
break;
}
return Bitmap.createScaledBitmap(srcBmp, side, side, true);
}
I have this weird problem that seems to only happen on one of my tablets that runs android 6.
I have this chunk of code to add a photo taken from the camera to a recycler view
1) I create a file object onto device (this is the photo)
2) I get the uri from that file
3) I create an intent passing in that uri as such
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (intent.resolveActivity(getActivity().getPackageManager()) != null)
// this is done in a fragment, everything else below is in the if statement
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(take_photo_intent, option_int);
I know MediaStore.EXTRA_OUTPUT does NOT return anything to onActivityResult, but instead writes to the uri you pass in.
now inside of onActivityResult I have
Log.i("PHOTO", "path--->" + uri.getPath());
So I want to mention, when the program works or DOESN'T work, the uri ALWAYS has a path, one example of one of the paths is
/storage/emulated/0/data/20161212_175150797715155.jpg
so to continue on in the onActivityResult
5) create bitmap based on uri path to use it later on
BitmapFactory.Options bitmap_options = new BitmapFactory.Options();
bitmap_options.inPreferredConfig = Bitmap.Config.ARGB_8888;
*************** problem here ****************
Bitmap temp = BitmapFactory.decodeFile(uri.getPath());
the temp bitmap returns null SOME of the time, let me explain
when I take the photo, the asus android 6 tablet shows two buttons when you take the photo, one button to discard the photo, another to keep the photo.. here is the weird part, ON THAT screen, if I wait like 5-15 seconds before pressing the button to keep the photo, the bitmap will NOT be null, but if I take a photo and immediately accept it, the a bitmap is null.
now as said before, does not matter how i do it, if the bitmap comes out null, or it does not come out null, it always has a path before passing it into the bitmapdecode function (which is weird)
I have no clue if it is the camera software, the physical camera hardware, an android 6 bug....
I also want to say, I am not sure if this is the best code but it has worked on 4 other android devices ( phones, tablets) it is just this ONE tablet that is a asus and only one with android 6, it works fine with everything else
EDIT:
I tried this as suggested
Bitmap TEMP = null;
try {
TEMP = BitmapFactory.decodeStream(getContext().getContentResolver().openInputStream(uri));
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
with no luck
EDIT 2:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == Activity.RESULT_OK) {
BitmapFactory.Options bitmap_options = new BitmapFactory.Options();
bitmap_options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap TEMP = BitmapFactory.decodeFile(uri.getPath());
Bitmap bitmap_image = ThumbnailUtils.extractThumbnail(TEMP, THUMBNAIL_SIZE, THUMBNAIL_SIZE);
if (bitmap_image == null)
Log.i("PHOTO", "BITMAP THUMBNAIL NULL");
setAdapterBitmap(bitmap_image, uri.getPath(), 1);
}
}
SOLUTION:
I had this method to create a File object and turn it into a URI Object
private File createFileForImage() throws IOException {
String file_creation_time = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
String image_file_name = DIR_NAME + "_" + file_creation_time;
File storage_dir = new File(String.valueOf(Environment.getExternalStorageDirectory()), DIR_NAME);
if (!storage_dir.exists())
{
if(!storage_dir.mkdir())
{
return null;
}
}
return File.createTempFile(image_file_name, ".jpg", storage_dir);
}
then I used this after it was return
uri = Uri.fromFile(image_file);
but for some reason this was working but it had a slight delay that cause bizarre behavior as stated in the original post
Jan Kaufmann suggestion seem to work, to i made some small modifications
private Uri getOutputMediaUriFromFile()
{
File photo_storage_dir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), DIR_NAME);
if (!photo_storage_dir.exists())
{
if (!photo_storage_dir.mkdirs())
{
return null;
}
}
String file_creation_time = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
String image_file_name = "ipr" + "_" + file_creation_time;
File photo = new File(photo_storage_dir.getPath() + File.separator + image_file_name + ".jpg");
return Uri.fromFile(photo);
}
It does essentially the same thing, but for some reason this doesn't cause the issue I was having, at least not with my testing.
I am glad this now works but can anyone explain why this worked?
Make sure the image size is not too big. If needed,subsample the original image to save memory
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inSampleSize = 8;
Make sure you have permission for
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Or try constructing your URI into something like this:
Uri uri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE, "myimage"); where getOutputMediaFileUri is :
private File getOutputMediaFile(int type, String imgname) {
// External sdcard location
//public static String DIRECTORY_PICTURES = "Pictures";
File mediaStorageDir = new File(
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), IMAGE_DIRECTORY_NAME);
// Create the storage directory if it does not exist
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d(IMAGE_DIRECTORY_NAME, "Oops! Failed create "
+ IMAGE_DIRECTORY_NAME + " directory");
return null;
}
}
File mediaFile;
if (type == MEDIA_TYPE_IMAGE) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + imgname + ".jpg");
} else {
return null;
}
return mediaFile;
}
Try this dude:
TEMP = MediaStore.Images.Media.getBitmap(context.getContentResolver(), YOUR_URL);
If this is not work, I guess that you didn't create a temp file to save the image.
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
try{
GEY_FILE;
}catch (Exception ex){
Log.i(TAG, "run: " + ex);
}
}
}, 15000);
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
Hey so I have been trying to fix this problem for a couple of hours and checked a lot of the other questions extensively in an attempt to remedy this problem.
So in my Main Activity I have two activities that both either grabs an image from the gallery or is a photo depending on the button
public void DoTakePhoto(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(intent, TAKE_PICTURE);
}
}
public void DoShowSelectImage(View v) {
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, SELECT_PICTURE);
}
So I know the problem likes on my onActivityResult where the data seems to be null I tried using the super so it gets back the lost activity or checking to see if data is
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == SELECT_PICTURE || requestCode == TAKE_PICTURE && null != data) {
if (resultCode == RESULT_OK && null != data) {
Uri selectedimage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedimage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
mImageFullPathAndName = cursor.getString(columnIndex);
cursor.close();
jobID = "";
File file = new File(mImageFullPathAndName);
Bitmap mCurrentSelectedBitmap = decodeFile(file);
if (mCurrentSelectedBitmap != null) {
ivSelectedImg.setImageBitmap(mCurrentSelectedBitmap);
int w = mCurrentSelectedBitmap.getWidth();
int h = mCurrentSelectedBitmap.getHeight();
int length = (w > h) ? w : h;
if (length > OPTIMIZED_LENGTH) {
float ratio = (float) w / h;
int newW, newH = 0;
if (ratio > 1.0) {
newW = OPTIMIZED_LENGTH;
newH = (int) (OPTIMIZED_LENGTH / ratio);
} else {
newH = OPTIMIZED_LENGTH;
newW = (int) (OPTIMIZED_LENGTH * ratio);
}
mCurrentSelectedBitmap = rescaleBitmap(mCurrentSelectedBitmap, newW, newH);
}
mImageFullPathAndName = SaveImage(mCurrentSelectedBitmap);
}
}
}
}
So my Error
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.example.zola.capchem/com.example.zola.capchem.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.getScheme()' on a null object reference
I have tried most of the things on this site but the app still ends up crashing. No clue what to do here.
Your code would crash if you try to take a picture from the camera.
The method for taking a photo via camera and obtaining a photo from the gallery/file system is quite different.
For doing both, you fire an intent with an ACTION and some extras. You launch an activity for a result with this intent. The result is returned to you in onActivityResult() via the intent passed to it.
However, the result stored in the returned intent is different for both cases.
1) Taking a photo through camera: The bitmap itself of the image taken is returned to you, as an extra in the intent bundle. You can access it as:
Bundle extras = data.getExtras();
Bitmap bitmap = (Bitmap) extras.get("data");
//use bitmap however you like...
2) Select a photo from gallery: You may or may not get a URI of the selected image (via data.getUri()). In case you get an URI, you can obtain your image through that URI. However, this uri may be null sometimes, in which case the android system has chosen to write the image on to the image URI that you had passed as an intent extra while launching the activity for result.
Hence, first, define a temporary URI and use it to launch the activity to select image from gallery:
private URI getTempFile()
{
if (isExternalStorageWritable())
{
File file = new File(Environment.getExternalStorageDirectory(), "temporary_file.jpg");
try
{
if(!file.exists())
{
file.createNewFile();
}
} catch (IOException e)
{
}
return Uri.fromFile(file);
} else
{
return null;
}
}
public void DoShowSelectImage(View v) {
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
i.putExtra(MediaStore.EXTRA_OUTPUT, getTempFile());
startActivityForResult(i, SELECT_PICTURE);
}
and inside your onActivityResult:
Uri selectedimage = data.getData();
if(selectedimage == null)
{
selectedimage = getTempFile();
}
You need to handle both these cases seperately in your onActivityResult:
if(result == RESULT_OK && data != null)
{
Bitmap mCurrentSelectedBitmap;
if(requestCode == SELECT_PICTURE)
{
Uri selectedimage = data.getData();
if(selectedimage == null)
{
selectedimage = getTempFile();
}
......
......
mCurrentSelectedBitmap = decodeFile(file);
}
else if(requestCode == TAKE_PICTURE)
{
Bundle extras = data.getExtras();
mCurrentSelectedBitmap = (Bitmap) extras.get("data");
}
.......
.......
//Do your thing
}
NOTE: you might need to add permission in your manifest to write to external storage
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />