App Crashing After Selecting Picture From Gallery - java

So whenever I select a picture from the gallery in my app, it crashes. Here is the code for the button to the gallery and selected picture to the imageview.
pickImageButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
openGallery();
}
});
}
private void openGallery() { //opens the gallery
Intent gallery =
new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(gallery, PICK_IMAGE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == PICK_IMAGE) {
Uri imageUri = data.getData();
imageView2.setImageURI(imageUri);
}
}
Please comment if you need more information, I desperately need help, as this is a very major roadblock for me.

Without the Logs, I can recommend on the following:
Make sure that pickImageButton isn't null.
Make sure that imageView2 isn't null.
Make sure that in manifest your Activity is in:
android:launchMode="singleTop"

Related

Automatically picks image from gallery according to its name java

I want to create an application that picks the image from gallery automatically according to its name. The user needs to sign in a signaturepad that he can save on the device and when he clicks on the button to save the image, the app automatically picks this image from gallery and display it on an imageview. I created one where I need to pick the image manually (with a little bit of help), this is my code so far:
private void SelectImage()
{
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*"); //Can I define here the name of the image that I want to be selected?
startActivityForResult(intent, PICK_IMAGE_REQUEST);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && null != data){
imageUri = data.getData();
imageView1.setImageURI(imageUri);
}
}

How to upload and save an image from gallery. startActivityForResult deprecated

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

startActivity for result does not called on fragment

I'am try to use start Activity for result to pick photo from gallery but it does not called on fragment
I checked most solutions but no one work for me
please help me
newsImg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
intent=new Intent(Intent.ACTION_PICK);
intent.setType("image*//*");
getActivity().startActivityForResult(intent,GALLERY_INTENT);
}
});
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==GALLERY_INTENT && resultCode== Activity.RESULT_OK){
Uri uri=data.getData();
StorageReference filePath=mStorage.child("Photos").child(uri.getLastPathSegment());
filePath.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
downloadUri=taskSnapshot.getDownloadUrl();
Picasso.with(getActivity().getApplicationContext()).load(downloadUri).fit().centerCrop().into(newsImg);
Toast.makeText(getActivity().getApplicationContext(),"success",Toast.LENGTH_SHORT).show();
publishBtn.setEnabled(true);
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Toast.makeText(getActivity().getApplicationContext(),"fail",Toast.LENGTH_SHORT).show();
}
});
}
Try using this and then find out, before giving my answer I would suggest you to be please do some research or search the exact thing in short form like you want to use the use the gallery intent, just use the image picker in android or gallery intent in android. For going from the fragment we use the getContext() rather than using ActivityIntent like MainActivity.this.
For Calling the startActivity for result do like this :
private final int REQUEST_CODE = 1
class ImagePicker extends Fragment{
/*here we are calling the start activity for result
supposing you know how to use the gallery intent so just giving you
the answer what you want */
Intent intent = new Intent(getContext(), YourClass.class);
startActivityForResult(intent, REQUEST_CODE);
}
And in onActivityResult() you get the result in the same Fragment or Activity after finishing this.
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
//get your gallery image uri data here
}
}
Hope it helps and please refer to this link to read more about the how to get the data in the same Activity.
About startAcitivityForResult in android
Hope that helps! Thanks
EDITS
Since your code seems fine for gallery intent but I think there is problem with your gallery intent. Try this also and see if it works.
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, RESULT_CODE;
Here specifically I have used the inten.setType("image/*") and one more thing, make this simple, do not use getActivity.startActivityForResult() just make it simple, startAcitivityForResult() will also work fine and use getContext() despite of doing getActivity().getApplicationContext(). Try amending this thins things and tell me whether it works or not!

How to record video using Intents?

I need to unserstand how I can record video programatically. Now I use this construction:
public class AndroidLearningActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.main);
Intent captureVideoIntent = new Intent(android.provider.MediaStore.ACTION_VIDEO_CAPTURE);
startActivityForResult(captureVideoIntent, 100);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Uri uri=data.getData();
Log.e("result", "result:"+resultCode);
}
}
When the application is opened then the camera will be opened too. I have record some video, but if I press "back" button on the device then the application crushes. Please, explain me, how can I do it? Thank you.
looks as you are pressing back key and data (intent) not get set.so data may be null here
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
Uri uri=data.getData();
Log.e("result", "result:"+resultCode);
}
}
You have problem in this statement
Uri uri=data.getData();
Log.e("result", "result:"+resultCode);
When you will press back button recording will be cancelled and you will get data.getData as null since no recording is done.So change your code to following.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data != null) {
Uri uri = data.getData();
Log.e("result", "result:" + resultCode);
}
super.onActivityResult(requestCode, resultCode, data);
}

How do I access the full size image taken with the camera from an Android Intent?

My button's onclick listener is as follows:
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
}
});
And the result is handled as follows:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_PIC_REQUEST) {
if (resultCode == Activity.RESULT_OK) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ImageView Preview = (ImageView) findViewById(R.id.PreviewImage1);
Preview.setImageBitmap(thumbnail);
Preview.setVisibility(View.VISIBLE);
}
}
}
I got the thumbnail working but how do I access the full image so that I can do some manipulations? I want to avoid saving the file if possible.
Here is a working example: http://achorniy.wordpress.com/2010/04/26/howto-launch-android-camera-using-intents/
Getting the full-sized image is not possible without saving to a file. Also it won't be a good idea, because having so big Bitmaps in memory will soon cause Out of memory exception.

Categories