I am selecting image and videos from gallery using intent.here is the code:
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
galleryIntent.setType("image/* video/*");
startActivityForResult(galleryIntent,RESULT_LOAD_IMG);
onActivityResult method:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
// VideoView videoView = (VideoView) findViewById(R.id.vidView);
// videoView.setVideoURI(selectedImage);
// videoView.start();
} else {
Toast.makeText(this, "You haven't picked Image",
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG)
.show();
}
}
My question is how can I save this selected picture or video in internal storage and then play from SD card? I tried Google, but was unable to find anything. Kindly help.
Related
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE ){
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if(resultCode == RESULT_OK) {
imageUri = result.getUri();
image_added.setImageURI(imageUri);
}
} else {
Toast.makeText(this, "Something went wrong , try again!", Toast.LENGTH_SHORT).show();
startActivity(new Intent(PostActivity.this , MainActivity2.class));
finish();
}
}
My code still works but when I select the image from gallery it does not get selected it directly gives the toast "Something went wrong , try again!"
i am trying to get the uri of the picture that have been taken from device camera. I just tried some of the advices from online but i could not manage to solve this problem here is the my code parts.
ImageView petImage;
Uri imageData;
petImage = view.findViewById(R.id.imgPetPic);
petImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
try {
startActivityForResult(takePictureIntent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
} catch (ActivityNotFoundException e) {
// display error state to the user
Toast.makeText(getActivity(), "Camera is Not Available", Toast.LENGTH_SHORT).show();
}
}
});
#Override
public void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
imageData = data.getData();
Bitmap bp = (Bitmap) data.getExtras().get("data");
petImage.setImageBitmap(bp);
} else if (resultCode == RESULT_CANCELED)
Toast.makeText(getActivity(), "Cancelled", Toast.LENGTH_LONG).show();
}
}
After that code parts worked. ımageData variable is still null. How do i fix this.
I want to select multiple images from gallery but after selecting images when I click on open the app crashes.
My code:
#Override
protected void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
if (resultCode == RESULT_OK) {
try {
final Uri imageUri = data.getData();
final InputStream imageStream = getContentResolver().openInputStream(imageUri);
final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
Toast.makeText(this, "images pick"+imageUri, Toast.LENGTH_SHORT).show();
TextView txtview = findViewById(R.id.imguri);
txtview.setText(String.valueOf(imageUri));
Intent intent = new Intent(MainActivity.this,ConvertActivity.class);
intent.putExtra("ImageUri",imageUri.toString());
startActivity(intent);
// image_view.setImageBitmap(selectedImage);
} catch (FileNotFoundException e) {
e.printStackTrace();
Toast.makeText(MainActivity.this, "Something went wrong", Toast.LENGTH_LONG).show();
}
}else {
Toast.makeText(MainActivity.this, "You haven't picked Image",Toast.LENGTH_LONG).show();
}
}
///pick gallery code
public void pickGallery() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE);
}
your Intent
Intent chooserIntent = new Intent();
chooserIntent.setType("image/*");
chooserIntent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE,true);
chooserIntent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(chooserIntent, "Select
Picture"), 100);
Activity Result
override fun onActivityResult(requestCode: Int, resultCode: Int, data:
Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == 100 && resultCode == RESULT_OK && data!=null) {
val clipdata = data.clipData
if(clipdata!=null){
for (i in 0 until data?.clipData!!.itemCount) {
val uri = data?.clipData?.getItemAt(i)?.uri
songlist.add(uri)}
}else{
val uri = data?.data
songlist.add(uri)
}
}
I am trying to use a adding button to get the photo from the gallery and put it to RecyclerView with text. When the user selected image form the gallery, the application will return the image path, but now, I no idea how to use the path to get the image and put it to RecyclerView to display with text, here is my coding:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK
&& null != data) {
Uri selectedImage = data.getData();
Toast.makeText(getActivity(), "Yor are picked image from" + selectedImage, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getActivity(), "You haven't picked image.",
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Toast.makeText(getActivity(), "Something went wrong.", Toast.LENGTH_LONG)
.show();
}
}
Sorry for my English. Thank you for help.
Here is my adding button
In your adapter class you have to implement add method, something like this:
public void addImage(String imagePath) {
imagesPaths.add(imagePath);
notifyDataSetChanged();
}
notifyDataSetChanged() is crucial, because it calls for update on RecyclerView.
So in your onActivityResult you just have to pass image path to adapter:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK
&& null != data) {
Uri selectedImage = data.getData();
// Here you pass image path
yourAdapter.add(selectedImage.toString());
Toast.makeText(getActivity(), "Yor are picked image from" + selectedImage, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getActivity(), "You haven't picked image.",
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Toast.makeText(getActivity(), "Something went wrong.", Toast.LENGTH_LONG)
.show();
}
}
In my app, I have a camera function. User can capture image or pick from gallery.
Activity B
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case RESULT_LOAD_IMAGE:
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK & null != data) {
selectedImage = data.getData();
imageView.setImageURI(selectedImage);
}
break;
case REQUEST_IMAGE_CAPTURE:
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
try {
selectedImage = imageUri;
getContentResolver().notifyChange(selectedImage, null);
imageView.setImageURI(null);
imageView.setImageURI(imageUri);
} catch (Exception e) {
Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT)
.show();
}
}
}
}
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent returnIntent = new Intent();
if(selectedImage!=null) {
returnIntent.putExtra("img_uri", selectedImage.toString());
}
setResult(Activity.RESULT_OK, returnIntent);
finish();
}
});
When submit button is clicked, it will return to previous activity.
Activity A
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == PROJECT_REQUEST_CODE) {
imgURI = Uri.parse(data.getStringExtra("img_uri"));
if (mClickedPosition == -1) {
if (obj != null)
obj.addNewItem(imgURI);
} else {
if (obj != null)
obj.changeItem(mClickedPosition, imgURI);
}
}
}
}
My question now is how to pass a default image to Activity A If
user didn't select any image ?
This is the xml imageView in Activity B
<ImageView
android:paddingTop="30dp"
android:layout_gravity="center"
android:layout_width="330dp"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="#mipmap/no_image"
android:scaleType="fitXY"
android:id="#+id/imageView"
android:layout_weight="0.50" />
If no image selected, I want #mipmap/no_image return to Activity B. Is it possible ? Thanks.
You can pass an Uri of your image stored in resources by the next way:
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent returnIntent = new Intent();
if(selectedImage!=null) {
returnIntent.putExtra("img_uri", selectedImage.toString());
} else {
returnIntent.putExtra("img_uri", Uri.parse("android.resource://your.package.name/mipmap/no_image").toString());
}
setResult(Activity.RESULT_OK, returnIntent);
finish();
}
});
Then you can use it as usual Uri to show the image in ImageView.
ByDefault set value of selectedImage to "default".
Now in your addNewItem() and changeItem() check value passed in imageUri
if(imageUri.equalsIgnoreCase("default"))
// set your default image that is #mipmap/no_image
else
// set image you are getting from imageUri
Code:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case RESULT_LOAD_IMAGE:
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK & null != data) {
selectedImage = data.getData();
imageView.setImageURI(selectedImage);
}else{
//set default
}
break;
case REQUEST_IMAGE_CAPTURE:
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
try {
selectedImage = imageUri;
getContentResolver().notifyChange(selectedImage, null);
imageView.setImageURI(null);
imageView.setImageURI(imageUri);
} catch (Exception e) {
Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT)
.show();
}
}else{
//set default
}
default{
//set default
}
}
You don't need to return a default image to Activity A. Like others wrote, you just have to prepare for the condition when the user doesn't select any image.
Modify your onActivityResult for Activity A like this:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(data!=null){
switch (requestCode) {
case RESULT_LOAD_IMAGE:
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK) {
selectedImage = data.getData();
imageView.setImageURI(selectedImage);
}
break;
case REQUEST_IMAGE_CAPTURE:
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
try {
selectedImage = imageUri;
getContentResolver().notifyChange(selectedImage, null);
imageView.setImageURI(imageUri);
} catch (Exception e) {
Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT).show();
}
}
break;
default:
//Your default image goes here, when the user doesn't select either of the two options
imageView.setImageResource(R.drawable.<default_image>);
break;
}
}