I need to view the image in android default image viewer when i execute the following code it's shows the album list instead Of particular image. please correct me.
MediaScannerConnection.scanFile(this, new String[] {receiptPath}, new String[] {"image/*"}, new MediaScannerConnection.OnScanCompletedListener()
{
#Override
public void onScanCompleted(String path, Uri uri)
{
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
intent.setType("image/*");
startActivity(intent);
}
});
I have found the answer for my question see the answer below
#Override
public void onScanCompleted(String path, Uri uri)
{
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "image/*");
startActivity(intent);
}
after analyse the android Intent source code my learning is, if you set uri in Intent constructor then set type, the data uri automatically set to null, so i can't access the particular image in image viewer.
Refer the source
Related
I am using Android Intent Chooser to select a photo from gallery with following code.
ivAvatar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Bir fotoğraf seçin ..."), 1);
}
});
After selection using the path I fill the imageview with following code :
Uri selectedImageUri = data.getData();
imagepath = ImagePathUtil.getPath(getApplicationContext(), selectedImageUri);
Bitmap bitmap = BitmapFactory.decodeFile(imagepath);
ivAvatar.setImageBitmap(bitmap);
selectedU = selectedImageUri;
File f = new File(String.valueOf(selectedU));
if(f.exists())
{
int i = 1;
}
Image can be viewed without any problems, but the File object I create afterwards File 's exists() method always return false.
It is returning false because the file will be null. Please look at the below post for more details on how to get the real path from URI.
URI from Intent.ACTION_GET_CONTENT into File
Want to pick image files from a specific folder vaibhav but have all the files of gallery instead.
Code
File dir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "Vaibhav/");
Log.e("Dir path",dir.toString());
dir.exists();
Uri uri = Uri.parse(dir.toString());
Intent browseIntent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.getContentUri(uri.toString()));
browseIntent.setType("image/jpg");
startActivityForResult(browseIntent, BROWSE_REQUEST);
Following Code Just Works Fine:
public void openFolder()
{
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath()
+ "/Vaibhav/");
intent.setDataAndType(uri, "image/jpg");
startActivity(Intent.createChooser(intent, "Open folder"));
}
Use
browseIntent.setDataAndType(uri, "image/.");//Loads all image files
Instead of
browseIntent.setType("image/jpg");
I want to show a photo in android gallery, and be able to slide throw the others photos on that folder.
Intent intent = new Intent(Intent.ACTION_VIEW);
File f = new File(path);
intent.setDataAndType(Uri.parse("file://" + f.getAbsolutePath()), "image/*");
mContext.startActivity(intent);
thats how i am doing it now, but wont let me slide throw the rest of the images in the folder.
i tried:
How to open one particular folder from gallery in android?
Built-in gallery in specific folder
Gallery with folder filter
Without any luck.
i would be really happy if someone have the solution.
Thanks!
Try This
Intent i=new Intent();
i.setAction(Intent.ACTION_VIEW);
i.setDataAndType(Uri.fromFile(new File(path)), "image/*");
startActivity(i);
See these Links
How can I use Intent.ACTION_VIEW to view the contents of a folder?
Android ACTION_VIEW Multiple Images
Java and Android: How to open several files with an Intent?
if this solves your problem. Also check
https://www.google.co.in/?gfe_rd=cr&ei=c5n9U6ruE7DO8gfXz4G4BA&gws_rd=ssl#q=view+like+gallery
also check Gallery widget
This question is from five years ago, However I want to give an answer that worked for me instead of the correct answer.
In order to show the photo and slide through the others photos, we need to give to the intent not the file uri but the media uri.
public void ShowPhoto(File imageFile) {
String mediaId = "";
String[] projection = new String[] {
MediaStore.Images.Media._ID,
MediaStore.Images.Media.DISPLAY_NAME
};
Cursor cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
projection, null, null, null);
while (cursor != null && cursor.moveToNext()) {
String name = cursor.getString((cursor.getColumnIndex(MediaStore.Images.ImageColumns.DISPLAY_NAME)));
if(name.equals(imageFile.getName())){
mediaId = cursor.getString((cursor.getColumnIndex(MediaStore.Images.ImageColumns._ID)));
break;
}
}
Uri mediaUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
if(!mediaId.equals("")){
mediaUri = mediaUri.buildUpon()
.authority("media")
.appendPath(mediaId)
.build();
}
Log.d("TagInfo","Uri: "+mediaUri);
Intent intent = new Intent(Intent.ACTION_VIEW, mediaUri);
startActivity(intent);
}
Try this way,hope this will help you to solve your problem.
final int OPEN_GALLERY = 1
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, ""), OPEN_GALLERY);
This question already has answers here:
Android: How to open a specific folder via Intent and show its content in a file browser?
(11 answers)
Closed 3 years ago.
I am trying to open a specific folder in android ?Is it possible to open a specific folder ???? this is the code i m using
config=(Button)findViewById(R.id.btn_cf);
config.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Intent intent = new Intent("Intent.ACTION_GET_CONTENT");
Uri uri = Uri.parse("mnt/sdcard/myfiles/allfiles/download");
intent.setDataAndType(uri, "*/*");
startActivity(Intent.createChooser(intent, "download"));
}
});
It works:
Uri selectedUri = Uri.parse(Environment.getExternalStorageDirectory() + "/myFolder/");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(selectedUri, "resource/folder");
startActivity(intent);
Have a nice codding :)
EDIT:
If the current solution doesn't help you, then these file/directory choosers libraries can be helpful:
https://android-arsenal.com/tag/35
try to replace your code with this line
btn.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath()
+ "/myFolder/");
intent.setDataAndType(uri, "text/csv");
startActivity(Intent.createChooser(intent, "Open folder"));
}
});
I found a solution in this GitHub repo
The code :
If you want open & browse file: FileBrowser.class
Intent intent = new Intent(activity, FileBrowser::class.java)
intent.putExtra(Constants.INITIAL_DIRECTORY, File(storageDirPath).absolutePath)
intent.putExtra(Constants.ALLOWED_FILE_EXTENSIONS,"*")
startActivityForResult(intent, CODE_INTENT )
If you want to get user chosen file's URI: FileChooser.class
Intent intent = new Intent(activity, FileChooser::class.java)
intent.putExtra(Constants.INITIAL_DIRECTORY, File(storageDirPath).absolutePath)
startActivityForResult(intent, CODE_INTENT )
Pick root:
Intent selectFile = new Intent();
selectFile.setAction("com.sec.android.app.myfiles.PICK_DATA_MULTIPLE");
selectFile.putExtra("CONTENT_TYPE", "*/*");
selectFile.addCategory(Intent.CATEGORY_DEFAULT);
startActivity(selectFile);
I am trying to share a video that is being created and stored on external sdcard whose path has been obtained by.
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES).getAbsolutePath()
I am using SEND_INTENT as follows:
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
shareIntent.setType("video/mp4");
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "My Subject");
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT,"My Text");
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(video_path));
startActivityForResult(Intent.createChooser(shareIntent, "Share Your Video"),SHARE_INTENT);
Problem:
While I share through gmail, it shows me compose window with video attached. But no size being shown of the video and when you either send or cancel the window, gmail will crash with inputstream NPE on contentresolver.
In case of youtube, it says you cannot upload videos from cloud service, my video clearly resides on the device.
In case of facebook, it is silently discarded. This works fine with wassup. :-)
Any ideas how to get this to work?
EDIT:
Video Path:
/storage/emulated/0/Movies/MyFolder/my-video_1378253389208.mp4
UPDATE
By adding file:/// suffix, gmail and facebook works fine.
Youtube is still cribbing about "Videos cannot be uploaded from cloud services".
The reason Youtube fails and others work is because it checks to see if it is a media file. It only thinks it is a media file if it has been scanned. Run the code below and it should work. It will also show up in the gallery. How to upload a temp file I do not know.
void publishScan() {
//mVidFnam: "/mnt/sdcard/DCIM/foo/vid_20131001_123738.3gp" (or such)
MediaScannerConnection.scanFile(this, new String[] { mVidFnam }, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.d(TAG, "onScanCompleted uri " + uri);
}
});
}
Based on Johan vdH's answer, the following code works on many sharing apps including Youtube, Facebook, WhatsApp and so on.
Path should be absolute path of video file. for eg. "/mnt/sdcard/DCIM/foo/vid_20131001_123738.3gp"
public void shareVideo(final String title, String path) {
MediaScannerConnection.scanFile(getActivity(), new String[] { path },
null, new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Intent shareIntent = new Intent(
android.content.Intent.ACTION_SEND);
shareIntent.setType("video/*");
shareIntent.putExtra(
android.content.Intent.EXTRA_SUBJECT, title);
shareIntent.putExtra(
android.content.Intent.EXTRA_TITLE, title);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
shareIntent
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
context.startActivity(Intent.createChooser(shareIntent,
getString(R.string.str_share_this_video)));
}
});
}
In addition to Johan vdH's answer, the Uri used in
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
Must be the one obtained from
public void onScanCompleted(String path, Uri uri)
Getting the Uri by
Uri uri = Uri.fromFile(new File(video_path));
Will not work. Youtube seems to like content:// but not file://
The best and easy method method to Share video on youtube, Use Provider to get Video Uri in Devices with Api >=24
private void VideoShareOnYoutube(String videoPath) {
try {
String csYoutubePackage = "com.google.android.youtube";
Intent intent = getPackageManager().getLaunchIntentForPackage(csYoutubePackage);
if (intent != null && !videoPath.isEmpty()) {
Intent share = new Intent(Intent.ACTION_SEND);
share.setPackage(csYoutubePackage);
Uri contentUri;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
contentUri = FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".my.package.name.provider", new File(videoPath));
}
else
{
contentUri = Uri.fromFile(new File(videoPath));
}
share.setType("video/*");
share.putExtra(Intent.EXTRA_STREAM, contentUri);
startActivity(share);
} else {
Common.showToast("Youtube app not installed!", activty);
}
} catch (Exception e) {
Log.e(TAG, "Youtubeexeption() :" + e.getMessage());
}
}
with this code you can try.
public void makeVideo(String nameVideo){
String type = "video/*";
// /storage/emulated/0/nameVideo.mp4
// if you have other path is necessary you change the path
String mediaPath = Environment.getExternalStorageDirectory() + File.separator + nameVideo;
createIntent(type, mediaPath);
}
private void createIntent(String type, String mediaPath) {
// Create the new Intent using the 'Send' action.
Intent share = new Intent(Intent.ACTION_SEND);
// Set the MIME type
share.setType(type);
// Create the URI from the media
File media = new File(mediaPath);
Uri uri = Uri.fromFile(media);
// Add the URI to the Intent.
share.putExtra(Intent.EXTRA_STREAM, uri);
// Broadcast the Intent.
startActivity(Intent.createChooser(share, "Share to"));
}
more information https://instagram.com/developer/mobile-sharing/android-intents/
Just a heads up as this caught me out when trying to share a video to Facebook via Intent Chooser that worked fine with Gmail and a few other Intents -
If there are spaces or other characters that get encoded to %20 for space for example, Facebook won't find the video if you try to play it.
I was using Video - 27 Apr 2016 - 16:59 pm.mp4 for instance, and spaces and colons were replaced, breaking the link inside Facebook. A thumbnail of the video would show when composing the share in Facebook, so I thought it was OK, but if you clicked to play it, it would say the file could not be found / played.
No such problem with Gmail, it sent and was playable once retrieved from email.
Here is the working solution for sharing videos, (code is in kotlin)
startActivity(
Intent.createChooser(
Intent().setAction(Intent.ACTION_SEND)
.setType("video/*")
.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
.putExtra(
Intent.EXTRA_STREAM,
getVideoContentUri(this, File(currentVideo.videoPath))
), resources.getString(R.string.share_video)
)
)
don't forget to add the following method,
/**
* Return the URI for a file. This URI is used for
* sharing of video.
* NOTE: You cannot share a file by file path.
*
* #param context Context
* #param videoFile File
* #return Uri?
*/
fun getVideoContentUri(context: Context, videoFile: File): Uri? {
var uri: Uri? = null
val filePath = videoFile.absolutePath
val cursor = context.contentResolver.query(
MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
arrayOf(MediaStore.Video.Media._ID),
MediaStore.Video.Media.DATA + "=? ",
arrayOf(filePath), null)
if (cursor != null && cursor.moveToFirst()) {
val id = cursor.getInt(cursor
.getColumnIndex(MediaStore.MediaColumns._ID))
val baseUri = Uri.parse("content://media/external/video/media")
uri = Uri.withAppendedPath(baseUri, "" + id)
} else if (videoFile.exists()) {
val values = ContentValues()
values.put(MediaStore.Video.Media.DATA, filePath)
uri = context.contentResolver.insert(
MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values)
}
closeCursor(cursor)
return uri
}
Happy Codng :)