Native video gallery does not display all the videos - android

My application filmed a video and then back to a screen that allows me to choose another video or gallery display videos.
The problem is that the gallery does not display all the videos I record, just one. I'm sure I record the videos because they appear in My Files.
The code used to display the gallery is as follows:
final static int REQUEST_VIDEO_CAPTURED = 1;
private Uri uriVideo;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.video_list);
Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
intent.setType("video/*");
startActivityForResult(intent, 1);
//I also try this
// Intent intent = new Intent(Intent.ACTION_PICK,
// android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
// intent.setType("video/*");
// startActivityForResult(intent, 1);
// and this
// Intent intent = new Intent();
// intent.setType("video/*");
// intent.setAction(Intent.ACTION_GET_CONTENT);
// startActivityForResult(Intent.createChooser(intent, "Select Video"),
// 1);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == REQUEST_VIDEO_CAPTURED) {
uriVideo = data.getData();
Toast.makeText(VideoList.this, uriVideo.getPath(), Toast.LENGTH_LONG).show();
ContentResolver res = getApplicationContext().getContentResolver();
Cursor cursor = MediaStore.Video.query(res, data.getData(), new String[] { MediaStore.Video.VideoColumns.DURATION });
if (cursor.moveToFirst()) {
duration = cursor.getString(0);
}
if (uriVideo != null) {
}
}
} else if (resultCode == RESULT_CANCELED) {
uriVideo = null;
finish();
}
}
I hope you understand, it's my first question.
Thank you very much!
Sorry for my bad English

you must request the system to update the media gallery

Related

How to upload particular image on particular imageView click from gallery and camera in android

i have 4 diffrent imageview and try to click on all to upload diffrent images like LIcence,Rc,Profile etc . I want to uploaded image from gallery or camera dialog..
like this layout.
like uper layout example
Please check my updated answer. This is just an example. Hope you understand from this
ImageView profile_pic;
private static final int SELECT_PICTURE1 = 100;
private static final int SELECT_PICTURE2 = 101;
private static final int SELECT_PICTURE3 = 102;
private static final int SELECT_PICTURE4 = 103;
picture1 = (ImageView) view.findViewById(R.id.picture1);
picture2 = (ImageView) view.findViewById(R.id.picture2);
picture3 = (ImageView) view.findViewById(R.id.picture3);
picture4 = (ImageView) view.findViewById(R.id.picture4);
picture1.setOnClickListener(this);
picture2.setOnClickListener(this);
picture3.setOnClickListener(this);
picture4.setOnClickListener(this);
#Override
public void onClick(View v) {
if (v.getId() == R.id.picture1) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE1);
}
if (v.getId() == R.id.picture2) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE2);
}
if (v.getId() == R.id.picture3) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE3);
}
if (v.getId() == R.id.picture4) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE4);
}
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE1) {
Uri selectedImageUri = data.getData();
if (null != selectedImageUri) {
String path = selectedImageUri.getPath();
Log.e("image path", path + "");
pricture1.setImageURI(selectedImageUri);
}
}
if (requestCode == SELECT_PICTURE2) {
Uri selectedImageUri = data.getData();
if (null != selectedImageUri) {
String path = selectedImageUri.getPath();
Log.e("image path", path + "");
picture2.setImageURI(selectedImageUri);
}
}
if (requestCode == SELECT_PICTURE3) {
Uri selectedImageUri = data.getData();
if (null != selectedImageUri) {
String path = selectedImageUri.getPath();
Log.e("image path", path + "");
picture3.setImageURI(selectedImageUri);
}
}
if (requestCode == SELECT_PICTURE4) {
Uri selectedImageUri = data.getData();
if (null != selectedImageUri) {
String path = selectedImageUri.getPath();
Log.e("image path", path + "");
picture4.setImageURI(selectedImageUri);
}
}
}
}
private void imageBrowse() {
if(camera){
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}else{
Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
// Start the Intent
startActivityForResult(galleryIntent, PICK_IMAGE_REQUEST);
}
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if(requestCode == PICK_IMAGE_REQUEST){
Uri picUri = data.getData();
filePath = getPath(picUri);
uri =picUri;
Log.d("picUri", picUri.toString());
Log.d("filePath", filePath);
imageView.setImageURI(picUri);
}
if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
}
}
}
EDIT
Since you are using recyclerview or listview kind of view, you can associate tag to each viewholder and then use that tag for resolving imageview. Now you can get unique tag based on position in view. For eg in recyclerview you can use getAdapterPosition().
Associate OnClicks of each imageview with a request code. In onActivityResult resolve them and put images accordingly.
I would like to suggest to refer this site, It's implementation is easy and straight forward...
http://www.coderzheaven.com/2012/04/20/select-an-image-from-gallery-in-android-and-show-it-in-an-imageview/
In addition to the perfectly working answer by #paras, if someone wants to include the Camera option also then please refer to the below steps:
Add a dialog box which pops-up on image click and asks to chose either Camera or Gallery:
final CharSequence[] items = {"Camera", "Gallery", "Cancel"};
AlertDialog.Builder builder = new AlertDialog.Builder(PhotoActivity.this);
builder.setTitle("Add Photo!");
builder.setItems(items, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int item) {
if (items[item].equals("Camera")) {
openCamera();
} else if (items[item].equals("Gallery")) {
openGallery();
} else if (items[item].equals("Cancel")) {
dialog.dismiss();
}
}
});
builder.show();
Use this method to open the Camera intent:
private void openCamera() {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, REQUEST_CAMERA);
}
Now in the onActivityResult() method, include this to check whether the intent is from Camera:
if (requestCode == REQUEST_CAMERA) {
Bundle bundle = data.getExtras();
final Bitmap bitmap = (Bitmap) Objects.requireNonNull(bundle).get("data");
imageView.setImageBitmap(bitmap);
}

How to pick an image from gallery in samsung

I want to build an app can choose photo from the gallery. this is the code i use
Intent photo_pick = new Intent(Intent.ACTION_PICK);
photo_pick.setType("image/*");
startActivityForResult(photo_pick , PICK_PHOTO_INTENT );
this code i had try and it work on XiaoMi, Huawei phone. But when it work on samsung the path it return is the error path cannot to get the photo.
How to improve it to let the samsung phone also can work?
Check Below code for choose photo from the gallery,
private static final int REQUEST_PROFILE_ALBUM = 1;
Intent int_album = new Intent(Intent.ACTION_PICK);
int_album.setType("image/*");
int_album.putExtra(MediaStore.EXTRA_OUTPUT, img_url);
startActivityForResult(int_album, REQUEST_PROFILE_ALBUM);
After Select Image onActivityResult is called,
if (requestCode == REQUEST_PROFILE_ALBUM && resultCode == Activity.RESULT_OK && data != null) {
Uri selectedImage = data.getData();
String[] projection = {MediaStore.Images.Media.DATA};
Cursor cursor = activity.getContentResolver().query(selectedImage, projection, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(projection[0]);
String picturePath = cursor.getString(columnIndex);
}
Try this it may helps you:
Button btn_selectimage = (Button) findViewById(R.id.btn_selectimage);
btn_selectimage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
img_capture.setVisibility(View.VISIBLE);
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
}
});
Code for xml:
<Button
android:id="#+id/btn_selectimage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Select Picture"/>
Pick a image like this
private void showFileChooser() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
}
Once you pick a image onActivityResult is called automatically
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
}
}
try this code:
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
if (intent.resolveActivity(getPackageManager()) != null) {
// Bring up gallery to select a photo
startActivityForResult(intent, 2);
}else{
Toast.makeText(UserProfileActivity.this,"No Gallery app found!", Toast.LENGTH_SHORT).show();
}
In order to execute below code, you will require to get request persmission only if android version is higher then lolipop
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, 500);

Overview of the videos on my phone, pick one and view it in my app

I want to get an overview of the videos on my phone, pick one and view it in my app. I'm almost there.. just for the last bit. When i click on the video I return to my app, but the video doesn't show. What am I missing?
private static int RESULT_LOAD_VIDEO = 1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.video);
Button buttonLoadImage = (Button) findViewById(R.id.buttonLoadPicture);
buttonLoadImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("video/*");
startActivityForResult(photoPickerIntent, RESULT_LOAD_VIDEO);
/* Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_VIDEO);*/
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_VIDEO && resultCode == RESULT_OK && null != data) {
Uri selectedVideo = data.getData();
String[] filePathColumn = { MediaStore.Video.Media.DATA };
Cursor cursor = getContentResolver().query(selectedVideo,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
VideoView imageView = (VideoView) findViewById(R.id.video1);
imageView.setVideoPath(picturePath);
}
}
I think you need to at least call imageView.start() to play the video (assuming you have everything else set up properly).
You can also invoke installed Android video player to play the video with just a few lines of code.
Uri fileUri = Uri.fromFile(videoFile);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(fileUri,
URLConnection.guessContentTypeFromName(fileUri.toString()));
startActivity(intent);
Intent chooser will show up to let user pick his preferred video player. I wrote a blog to discuss how to do it in detail, http://software.intel.com/en-us/blogs/2014/03/20/video-playing-with-android-media-player. Hope it helps.

how to open the gallery?

I need to open the images gallery via code in my app.(only opening the gallery, the user is not going to select any image). I searched and found lots of ways but some of them worked only for selecting an image and other ways never worked.e.g.
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);//
startActivityForResult(Intent.createChooser(intent, "Select Picture"),SELECT_IMAGE);
or
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(
"content://media/internal/images/media"));
startActivity(intent);
how may I open the gallery?
public void pickPhoto(View view)
{
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"),1);
}
Here:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// Image captured and saved to fileUri specified in the Intent
Toast.makeText(this, "Image saved to:\n" +
data.getData(), Toast.LENGTH_LONG).show();
Uri curImageURI = data.getData();
Bitmap bit = getRealPathFromURI(curImageURI);
imageView.setImageBitmap(bit);
} else if (resultCode == RESULT_CANCELED) {
// User cancelled the image capture
} else {
// Image capture failed, advise user
}
}
and
public String getRealPathFromURI(Uri contentUri) {
String[] proj = { MediaStore.Images.Media.DATA };
#SuppressWarnings("deprecation")
android.database.Cursor cursor = managedQuery(contentUri, proj, null,
null, null);
int column_index;
try {
column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} catch (Exception e) {
return null;
}
}
CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE is the final number in the "startActivityForResult(Intent.createChooser(intent, "Select Picture"),1);"
in this case '1' so you'd need to change it to 1 or declare it as a global variable

Not getting able to call onActivityResult Method

hi i am not able to getting call to method onActivityResult after below code.
private void ImageChooseOptionDialog() {
Log.i(TAG, "Inside ImageChooseOptionDialog");
String[] photooptionarray = new String[] { "Take a photo",
"Choose Existing Photo" };
Builder alertDialog = new AlertDialog.Builder(TabSample.tabcontext)
.setItems(photooptionarray,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int which) {
if (which == 0) {
Log.i(TAG, "Which" + which);
Log.i(TAG,
"Inside ImageChooseOptionDialog Camera");
_path = Environment
.getExternalStorageDirectory()
+ File.separator
+ "TakenFromCamera.png";
Log.d(TAG, "----- path ----- " + _path);
media = _path;
// File file = new File(_path);
// Uri outputFileUri = Uri.fromFile(file);
// Intent intent = new Intent(
// android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
// intent.putExtra(MediaStore.EXTRA_OUTPUT,
// outputFileUri);
// startActivityForResult(intent, 1212);
} else if (which == 1) {
Log.i(TAG, "Which" + which);
Log.i(TAG,
"Inside ImageChooseOptionDialog Gallary");
// Intent intent = new Intent();
// intent.setType("image/*");
// intent.setAction(Intent.ACTION_GET_CONTENT);
// startActivityForResult(Intent
// .createChooser(intent,
// "Select Picture"), 1);
Intent intent = new Intent(
Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
// intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent
.createChooser(intent,
"Select Picture"), 1);
Log.i(TAG, "end" + which);
}
}
});
alertDialog.setNeutralButton("Cancel",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.create().show();
}
and this is my onActivityResult method:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.i(TAG, "Inside Onactivity Result");
if (resultCode == RESULT_OK) {
if (requestCode == 1) {
Log.i(TAG, "Inside if else Onactivity Result");
// currImageURI is the global variable I’m using to hold the
// content:// URI of the image
Uri currImageURI = data.getData();
String path = getRealPathFromURI(currImageURI);
Log.i(TAG, "Inside Onactivity Result Image path" + path);
}
}
}
please let me knew where i am doing wrong. i am calling onActivityResult method from which=1 after dialog box appears.
but not getting any log inside onActivityResult method in logcat.
This is because, you may not be getting RESULT_OK. Always first check for request code and inside that check for result code. If you are retrieving image from gallery, try following code inside onActivityResult():
if (requestCode == TAKE_PICTURE_GALLERY) {
if (resultCode == RESULT_OK) {
final Uri selectedImage = data.getData();
final String[] filePathColumn = { MediaStore.Images.Media.DATA };
final Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
final int columnIndex = cursor
.getColumnIndex(filePathColumn[0]);
final String filePath = cursor.getString(columnIndex);
cursor.close();
}
}
And use the filePath wherever you want.
I hope this solves your problem. Thank you :)
UPDATE:
Use this code to start your gallery activity:
imagePathURI = Uri.fromFile(new File(<your image path>));
final Intent intent = new Intent(Intent.ACTION_PICK, imagePathURI);
intent.setType("image/*");
intent.putExtra(MediaStore.EXTRA_OUTPUT, imagePathURI);
startActivityForResult(intent, TAKE_PICTURE_GALLERY);
When you want to retrieve image from gallery, the MediaStore.EXTRA_OUTPUT refers to The name of the Intent-extra used to indicate a content resolver Uri to be used to store the requested image or video. So here, you have to pass the image path where u'll receive your image.
imagePathURI = Uri.fromFile(new File(<your image path>)); // here a file will be created from your image path. And when you'll receive an image, u can access the image from this image path.

Categories

Resources