Reputation: 5705
I am using camera to capture image and set the returned bitmap into an imageview but it is crashing on OnePlus devices. I checked on few htc devices and it is working perfectly. I checked for solutions and they said to check for request code but I am already doing it. What can be the problem here?
Here is the code
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, 1);
}
});
and onActivityResult
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1 && resultCode == -1) {
image = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
// Must compress the Image to reduce image size to make upload easy
image.compress(Bitmap.CompressFormat.PNG, 50, stream);
byte[] byte_arr = stream.toByteArray();
// Encode Image to String
encodedString = Base64.encodeToString(byte_arr, 0);
Uri selectedImageUri = data.getData();
img_path = getRealPathFromURI(selectedImageUri);
// Uri tempUri = getImageUri(getApplicationContext(), image);
imageView.setImageBitmap(image);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
}
}
and the stack trace of error
STACK_TRACE=java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.madhours/com.madhours.activities.ActivitySignUp}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.getPath()' on a null object reference at android.app.ActivityThread.deliverResults(ActivityThread.java:3659) at android.app.ActivityThread.handleSendResult(ActivityThread.java:3702) at android.app.ActivityThread.access$1300(ActivityThread.java:155) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1366) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5343) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.getPath()' on a null object reference at com.madhours.activities.ActivitySignUp.getRealPathFromURI(ActivitySignUp.java:134) at com.madhours.activities.ActivitySignUp.onActivityResult(ActivitySignUp.java:116) at android.app.Activity.dispatchActivityResult(Activity.java:6218) at android.app.ActivityThread.deliverResults(ActivityThread.java:3655) ... 10 more java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.getPath()' on a null object reference at com.madhours.activities.ActivitySignUp.getRealPathFromURI(ActivitySignUp.java:134) at com.madhours.activities.ActivitySignUp.onActivityResult(ActivitySignUp.java:116) at android.app.Activity.dispatchActivityResult(Activity.java:6218) at android.app.ActivityThread.deliverResults(ActivityThread.java:3655) at android.app.ActivityThread.handleSendResult(ActivityThread.java:3702) at android.app.ActivityThread.access$1300(ActivityThread.java:155) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1366) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5343) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
Upvotes: 0
Views: 1082
Reputation: 1007524
What can be the problem here?
The problem is that you are calling getData()
on the returned Intent
. Returning a Uri
is not part of the ACTION_MEDIA_STORE
contract:
The caller may pass an extra EXTRA_OUTPUT to control where this image will be written. If the EXTRA_OUTPUT is not present, then a small sized image is returned as a Bitmap object in the extra field. This is useful for applications that only need a small image. If the EXTRA_OUTPUT is present, then the full-sized image will be written to the Uri value of EXTRA_OUTPUT.
Given that you did not put EXTRA_OUTPUT
on the Intent
you used with startActivityForResult()
, the only result you will get is the Bitmap
in the data
extra.
If you want a full-size image written somewhere, provide a path to that location in EXTRA_OUTPUT
, then hold onto that path and use it when onActivityResult()
is called:
/***
Copyright (c) 2008-2016 CommonsWare, LLC
Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
by applicable law or agreed to in writing, software distributed under the
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
OF ANY KIND, either express or implied. See the License for the specific
language governing permissions and limitations under the License.
From _The Busy Coder's Guide to Android Development_
https://commonsware.com/Android
*/
package com.commonsware.android.camcon;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import java.io.File;
public class CameraContentDemoActivity extends Activity {
private static final String EXTRA_FILENAME=
"com.commonsware.android.camcon.EXTRA_FILENAME";
private static final String FILENAME="CameraContentDemo.jpeg";
private static final int CONTENT_REQUEST=1337;
private File output=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent i=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (savedInstanceState==null) {
File dir=
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
dir.mkdirs();
output=new File(dir, FILENAME);
}
else {
output=(File)savedInstanceState.getSerializable(EXTRA_FILENAME);
}
if (output.exists()) {
output.delete();
}
i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(output));
startActivityForResult(i, CONTENT_REQUEST);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putSerializable(EXTRA_FILENAME, output);
}
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
if (requestCode == CONTENT_REQUEST) {
if (resultCode == RESULT_OK) {
Intent i=new Intent(Intent.ACTION_VIEW);
i.setDataAndType(Uri.fromFile(output), "image/jpeg");
startActivity(i);
finish();
}
}
}
}
Upvotes: 1