I am currently using a photo gallery I built through GridLayout but this seems to use a lot of memory (OOME coming up) when dealing with a lot of Bitmaps. Therefore, I am trying to use the Intent.ACTION_GET_CONTENT to open photos on the device and select photos through there.
The camera function works correctly as it takes the photo, returns to the onActivityResult function, and updates it to my Gridlayout photo gallery.
I am currently on a Nexus 7 running Android 4.4 (KitKat)
[PROBLEMS]
When I try to view photos through Intent.ACTION_GET_CONTENT, none of the new photos I just took through the application are there.
Running Intent.ACTION_GET_CONTENT to filter results from a specific directory and file type doesn't seem to work.
[Code information]
MainForm is a (class)
photoPath = new File(directory, photoName)
directory is a (File) set to my current working directory
photoName is a (String)
CAMERA_CODE is an (int)
[MY CODE]
Here is my code for launching the camera:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cUri = Uri.fromFile(photoPath);
intent.putExtra(MediaStore.EXTRA_OUTPUT, cUri);
startActivityForResult(intent, CAMERA_CODE); // launch camera
[What I tried + Errors for Updating Photo Gallery library]
MediaScannerConnection
I tried to use this to update the photolist and then launch the intent, but it did not work (I may be using this incorrectly, but it was suggested in this post)
MediaScannerConnection.scanFile(MainForm.this, new String[] { directory.getAbsolutePath() },null, new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri){
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
intent.setData(uri);
intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent,"Select Picture"), 12345);
}
});
getContentResolver()
I read in this question to try using this method, but it doesn't seem to work either
MainForm.this.getContentResolver().notifyChange(MediaStore.Images.Media.INTERNAL_CONTENT_URI, null);
sendBroadcast
In this question and here to re-index photos on the device, the answer suggested using sendBroadcast
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.fromFile(directory)));
but I was greeted with this error
E/AndroidRuntime(30177): java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.intent.action.MEDIA_MOUNTED from pid=30177, uid=10122
What does this error mean?
[What I tried + Errors for ACTION_GET_CONTENT intent]
No matter what I put under intent.setData, opening the intent doesn't seem to open that folder, but instead it opens the android browser under the tab "Recent"
I followed this question
ANY TIPS OR SUGGESTIONS WOULD BE GREATLY APPRECIATED! If there are any other codes or log results I should post, please comment below!
Related
I'm creating a Sign-Up page from my app that has the option to obtain an image from the gallery using this code:
Intent pickImageFileIntent = new Intent(Intent.ACTION_PICK);
pickImageFileIntent.setType("image/*");
String pickTitle = getString(R.string.select_img_gallery);
Intent chooserIntent = Intent.createChooser(pickImageFileIntent, pickTitle);
startActivityForResult(chooserIntent, PICK_IMAGE);
After the user selects the image from the gallery I show that image in an ImageView using Glide, and it works fine. But when I try to use that image's Uri in another activity I get permission denied and the app crashes, so I think the Uri only has permissions for the Activity where ti was obtained. I tried to use ACTION_GET_DOCUMENT instead of ACTION_PICK and it works but the problems is in the Intent chooser it only shows the file explorer instead of the default media gallery and Google Photos, like when I use ACTION_PICK. Is there a way to use the Uri outside the activity while still using ACTION_PICK?
I am new to android development.
I am trying to open the default gallery app to view an image like this:
Uri u = Uri.parse((String) gridView.getAdapter().getItem(position);
Intent i = new Intent();
i.setDataAndType(u, "image/*");
i.setAction(Intent.ACTION_VIEW);
i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(i);
I provide the file paths to my images like this:
/storage/emulated/0/WhatsApp/Media/WhatsApp Images/IMG-20191023-WA0045.jpg
/storage/emulated/0/Pictures/9GAG/1565987862000.jpg
My App and Google Photos can display the image given this path, the Samsung Gallery( tested on Android Oreo) or the Xiaomi Gallery App (tested on Android X) can't find the image; also if i try to add "content://" or "file://" at the beginning.
I suspect that the apps want to have a Content URI or some kind of symbolic link without the "/storage/emulated/0/" part instead of a File URI. I searched for methods to get the content URI out of the file path, but didn't find any examples for Android X.
So what is the correct URI to pass on through the Intent and how do i get it?
Or maybe somebody knows a better way to just open the gallery app to a specific picture i provide by an absolute file path (Both External Storage and SD Card)?
Thank you in advance and greetings!
copied from this question here , this might work for you
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("content://media/external/images/media/16"))); /** replace with your own uri */
In my app I want to give users a way to pick a file from the app’s data directory. This is my code:
// use ACTION_OPEN_DOCUMENT because ACTION_GET_CONTENT will give us
// gallery and other stuff we don’t need
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
Uri uri = Uri.parse(getExternalFilesDir(null).getAbsolutePath());
Log.d(TAG, "Browsing " + uri.toString());
intent.setDataAndType(uri, "*/*");
// show the entire internal storage tree
intent.putExtra("android.content.extra.SHOW_ADVANCED", true);
startActivityForResult(intent, 42);
The logcat shows me that the URI that I am setting is file:///sdcard/Android/data/my.app/files, but the file picker UI defaults to the shared storage root (/sdcard).
The following code works (requires API 26+ as per the documentation, the intent is available from the API as DocumentsContract.EXTRA_INITIAL_URI):
// works only with this intent, at the expense of gallery etc. appearing
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
// apparently we need a valid content URI
Uri uri = Uri.parse("content://com.android.externalstorage.documents/document/primary%3AAndroid%2Fdata%2Fmy.app%2Ffiles");
intent.putExtra("android.provider.extra.INITIAL_URI", uri);
Log.d(TAG, "Browsing " + uri.toString());
intent.setType("*/*");
// show the entire internal storage tree
intent.putExtra("android.content.extra.SHOW_ADVANCED", true);
startActivityForResult(intent, 42);
intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, uri);
However, ACTION_GET_CONTENT causes all kinds of providers to appear, such as Gallery and Music, when all I need is the local file system (and in fact just the app’s private subtree). If I change the intent to ACTION_OPEN_DOCUMENT, the URI I supply is ignored.
How can I get the file picker UI to start in a directory of my choice, with only a minimal choice of content providers?
Edit: Testing this on Anbox, which I just realize is only at API 25—in fact, I need a way that works on APIs as low as 24.
There may not be a universally viable solution, but the following has worked on some builds (though not on others):
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
Uri uri = Uri.parse("content://com.android.externalstorage.documents/document/primary%3AAndroid%2Fdata%2Fmy.app%2Ffiles");
intent.setData(uri);
intent.setType("*/*");
intent.putExtra("android.provider.extra.INITIAL_URI", uri);
intent.putExtra("android.content.extra.SHOW_ADVANCED", true);
startActivityForResult(intent, 42);
uri must be a content URI for the com.android.externalstorage.documents provider. The URI path is /document/primary%3A, followed by the filesystem path to the folder to start in. The path must be relative to the shared storage root (i.e. drop the leading /sdcard/ or equivalent on the device and make sure the result does not start with a slash) and escaped.
The call to Intent#setData() does not help in setting the default location (unlike with some third-party file managers) but prevents unwanted storage providers (such as Gallery and Music) from being displayed.
The android.provider.extra.INITIAL_URI extra sets the initial URI, but this may not work prior to API 26 (although it does work on some flavors of Android).
The android.content.extra.SHOW_ADVANCED extra causes device storage to be available as a provider (otherwise, depending on the flavor of Android, it may require the user to select it or not be available at all).
Again, still not a perfect solution but the closest I managed to get.
I am using the following code to request an image from the user:
Intent pickIntent = new Intent();
pickIntent.setType("image/* video/*");
pickIntent.setAction(Intent.ACTION_GET_CONTENT);
pickIntent.addCategory(Intent.CATEGORY_OPENABLE);
// child intents to allow capture image and video
Intent capturePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Intent captureVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
// set max capture size
capturePhotoIntent.putExtra(MediaStore.EXTRA_SIZE_LIMIT, 20971520L); //20*1024*1024=20MB
captureVideoIntent.putExtra(MediaStore.EXTRA_SIZE_LIMIT, 20971520L); //20*1024*1024=20MB
// present import chooser
String chooserTitle = "Import Media From...";
Intent chooserIntent = Intent.createChooser(pickIntent, chooserTitle);
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[]{capturePhotoIntent, captureVideoIntent});
startActivityForResult(chooserIntent, MEDIA_CAPTURE);
This works on my Nexus 5, but crashes on my Galaxy S5. The crash occurs after I have taken the photo, and tap "Save," but before onActivityResult() is called. I have also tried supplying a URI via capturePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, mUri), but the same behavior occurs.
I get no stack trace for my code, since nothing is passed back to my activity; the activity just crashes.
I've been working on this for days. Any advice is appreciated.
I figured out the problem. My app was running out of memory. I solved this by adding android:largeHeap="true" to my AndroidManifest.xml, as well as redesigning the code that managed the picture data.
I am writing an Android application to upload a mp4 format video from sdcard to youtube using sharing Intent. It ran perfectly at the first time when I installed to my phone. However, after I restart the app, I cannot share it again. If i share to whatsapp, "sharing fail" apear. If I click youtube, I got "no media uri(s)" message on logcat. Reinstalling on same phone or restarting the phone cannot solve the problem.
Even when I install the app in same code to other android device, it only runs perfectly once. When i restart the app, same problem happened.
I got the code from this website:
Trouble with youtube upload
And this is my code:
findViewById(R.id.button2).setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("video/*");
ContentValues content = new ContentValues(4);
content.put(Video.VideoColumns.DATE_ADDED,
System.currentTimeMillis() / 1000);
content.put(Video.Media.MIME_TYPE, "video/mp4");
content.put(MediaStore.Video.Media.DATA, Environment.getExternalStorageDirectory().getAbsolutePath()+"/myVideo/myVideoTemp.mp4");
ContentResolver resolver = getBaseContext().getContentResolver();
Uri uri = resolver.insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, content);
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT,"");
sharingIntent.putExtra(android.content.Intent.EXTRA_TITLE,"");
sharingIntent.putExtra(android.content.Intent.EXTRA_STREAM,uri);
startActivity(Intent.createChooser(sharingIntent,"Share video"));
}
});
p.s. there is no error in red color on logcat
Problem solved after adding sharingIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Try the following to invoke the "share" applications:
//Use the URI that points to the video file, in my case, the video file is in local storage (sd card)
Uri fileUri = Uri.fromFile(videoFile);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
//Use the setDataAndType method to provide the URI and MIME type to the file
//Please note, you need to use setDataAndType method to make it work
intent.setDataAndType(fileUri,URLConnection.guessContentTypeFromName(fileUri.toString()));
startActivity(intent);
I have a blog to show how you can invoke video players from your app with a video file stored locally. It may help:
http://software.intel.com/en-us/blogs/2014/03/20/video-playing-with-android-media-player