My Intent activity starting code to open camera is as follow
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
getActivity().startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
Note:- This functionality is in a class extending dialogfragment class
My OnActivityResult method,which is in the Activity class,is as follows
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d("sdf",""+data);
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
mimageView = (ImageView) findViewById(R.id.profileimageView4);
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
//mimageView.setImageBitmap(imageBitmap);
}
}
The error showing in the log is as follows
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, result=-1, data=Intent { dat=content://media/external/images/media/22615 flg=0x1 }} to activity {www.vyrazu.com.purpleknights/www.vyrazu.com.purpleknights.Profile}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.os.Bundle.get(java.lang.String)' on a null object reference
What is the problem in this code.Thanks everyone for your time.
From what I can see, your code appears to be fine. However, it relies on ACTION_IMAGE_CAPTURE, and many camera apps have bugs. You might wish to try your app with a different camera app and see what the results are.
Related
I'm currently learning how to use android studio in java and am trying to make a social media app. I am currently creating an edit profile page where the user would update their details and upload a profile picture.
I have been following tutorials online and all of the ones I have come across use the startActivityForResult method. It has been crossed out and wont call the method as it is deprecated. But I don't know what to use instead.
`ProfileImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//open gallery
Intent OpenGalleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(OpenGalleryIntent, 1000);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #androidx.annotation.Nullable Intent data){
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 1000){
if(resultCode == Activity.RESULT_OK){
Uri imageUri = data.getData();
ProfileImage.setImageURI(imageUri);
UploadImageToFirebase(imageUri);
}
}
}
private void UploadImageToFirebase(Uri image){
StorageReference fileRef = storageReference.child("Profile.jpg");
fileRef.putFile(image).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Toast.makeText(Edit_Profile.this, "Image Uploaded", Toast.LENGTH_SHORT).show();
}
});![enter image description here](https://i.stack.imgur.com/padRc.jpg)`
I know there is an alternative but I don't understand how it works.
startActivityForResult is indeed deprecated in later versions of AndroidX Activity and Fragment APIs (while I believe you can still use it despite of warning). New way to get result from activity is registerForActivityResult.
In your code you would need to create a launcher, which will handle result (selected image)
private final ActivityResultLauncher<Intent> launcher = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
result -> {
if (result.getResultCode() == Activity.RESULT_OK
&& result.getData() != null) {
Uri photoUri = result.getData().getData();
//use photoUri here
}
}
);
and then launch this launcher in onClickListener
profileImage.setOnClickListener(view -> {
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
launcher.launch(intent);
});
Yes startActivityForeResult is deprecated.
Now you can use ActivityResultLauncher for the callbacks
https://developer.android.com/training/basics/intents/result#java
This question already has answers here:
How can I pass a Bitmap object from one activity to another
(10 answers)
Closed 4 years ago.
My main activity contains the imageview and editactivity has a button for changing the image in imageview .
I used the Intent to startActivityForResult and onActivityResult code is shown below
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
Uri uri = data.getData();
try {
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
View mainactivity;
ImageView imageprofile;
mainactivity= LayoutInflater.from(this).inflate(R.layout.activity_main,null);
imageprofile = (ImageView)mainactivity.findViewById(R.id.ProfileImage);
imageprofile.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
The image choosen is not set , please help
You should not pass Bitmap to another activity.
Passing the entire bitmap requires a lot of memory
Pass the URI to another activity and load if needed. Hope it helps!
This is my code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_crop);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent,CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// handle result of CropImageActivity
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (resultCode == RESULT_OK) {
((ImageView) findViewById(R.id.quick_start_cropped_image)).setImageURI(result.getUri());
Toast.makeText(
this, "Cropping successful, Sample: " + result.getSampleSize(), Toast.LENGTH_LONG)
.show();
} else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
Toast.makeText(this, "Cropping failed: " + result.getError(), Toast.LENGTH_LONG).show();
}
}
}
When I take a picture my app crashes. The exception on my phone is:
java.lang.RuntimeException: Failure delivering result
ResultInfo{who=null, request=203, result=-1, data=Intent { act=inline-
data (has extras) }} to activity
{com.example.michael.matcalc/com.example.michael.matcalc.Crop}:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.net.Uri
com.theartofdev.edmodo.cropper.CropImage$ActivityResult.getUri()' on a
null object reference
at android.app.ActivityThread.deliverResults(ActivityThread.java:4355)
...
I think that something goes wrong with the data. It crashes at this line:
((ImageView) findViewById(R.id.quick_start_cropped_image)).setImageURI(result.getUri());
The library I use is this: https://github.com/ArthurHub/Android-Image-Cropper
I would appreciate if somebody could help me.
This is because you're incorrectly using the library. You need to use the following code:
CropImage.activity()
.setGuidelines(CropImageView.Guidelines.ON)
.start(this);
instead of:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent,CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE);
It is because when you're calling Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); you're not specifically tell the Crop library to handle the image.
I have a problem. I want to get the user of my app to select an audio file from storage using ACTION_GET_CONTENT, and my mainActivity crashes at that point.
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("audio/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent,1);
audioPath = intent.getData().getPath(); //This is were the crash happens
audio = Uri.parse(audioPath);
}
I'm new in android programming and there is surely something that I fail to understand. The error is as follows: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.getPath()' on a null object reference
You have to override on Activity Result in your activity. DO NOT use the same intent like you have done. You have to use the Intent returned in onActivityResult method. So your code should be:
This should be your method to Pick an Audio
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("audio/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent,1); //This 1 is your request code remember it
}
Then Override the onActivityResult method as
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
//The intent from this method is the one you need to get data from!
if (requestCode == 1) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
audioPath = intent.getData().getPath();
audio = Uri.parse(audioPath);
}
}
}
For more information on this method and receiving data from Activity Result check this official training from Android. Happy Coding!
I am android beginner, my aim is select image and display on my ImageView, I use below code but in fragment onActivity result return null pointer exception.
This is my fragmentA.java
#Override
public void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch(requestCode) {
case SELECT_PHOTO:
if(resultCode == getActivity().RESULT_OK) {
try {
final Uri imageUri = imageReturnedIntent.getData();
Log.v("imageUri", imageUri.toString());
final InputStream imageStream = getActivity().getContentResolver().openInputStream(imageUri);
final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
image.setImageBitmap(selectedImage);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
break;
default:
initImage();
break;
}
}
I used below code for call gallery intent
image.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
if( Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT )
{
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent.createChooser(intent,"Select Picture"), SELECT_PHOTO);
}
else
{
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, SELECT_PHOTO);
}
}
});
This is my error report ..
java.lang.RuntimeException: Unable to resume activity {citycenter.caneda.com.citycenter/com.caneda.citycenter.myaccount.AccountMainActivity}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=65537, result=-1, data=Intent { dat=content://media/external/images/media/506 }} to activity {citycenter.caneda.com.citycenter/com.caneda.citycenter.myaccount.AccountMainActivity}: java.lang.NullPointerException
Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=65537, result=-1, data=Intent { dat=content://media/external/images/media/506 }} to activity {citycenter.caneda.com.citycenter/com.caneda.citycenter.myaccount.AccountMainActivity}: java.lang.NullPointerException
Caused by: java.lang.NullPointerException
at com.caneda.citycenter.myaccount.FragmentTabProfile.onActivityResult(FragmentTabProfile.java:217)
at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:163)
at com.caneda.citycenter.myaccount.AccountMainActivity.onActivityResult(AccountMainActivity.java:102)
at android.app.Activity.dispatchActivityResult(Activity.java:5305)
I think the line
resultCode == getActivity().RESULT_OK
is causing the problem
USE
resultCode == Activity.RESULT_OK
because RESULT_OK is static
However you can debug to test which code is causing NullPointerException
Actully answer is really silly, I have initialize
conditional based
if(saveInstaceState == null) { ivUpload = (ImageView) }
It should be outside of condition.
That's why it cause nullPointerException by ivUpload.
Thanks you so much for you support.