Is there a way to get file from ACTION_GET_CONTENT? - android

I'm trying to get file from google drive using ACTION_GET_CONTENT.
When I clicked the file, then the results are:
uri.getPath() : "/document/acc=1;doc=encoded=K45Kn19bK1yXpNssYJAhfwJ/4hgc5VmQJk2FWtVNoeM+c1vHUO4H"
uri.toString():"content://com.google.android.apps.docs.storage/document/acc%3D1%3Bdoc%3Dencoded%3DK45Kn19bK1yXpNssYJAhfwJ%2F4hgc5VmQJk2FWtVNoeM%2Bc1vHUO4H"
File recoveryFIle = new File(uri.toString());
file size : 0
How can I get the real file? I select the file from Google Drive.
getfile
Intent recovery = new Intent(Intent.ACTION_GET_CONTENT);
recovery.setType("*/*");
recovery.putExtra(Intent.EXTRA_SUBJECT,"Savelocation_Backup");
startActivityForResult(Intent.createChooser(recovery, "Share File"),3002);
result
Uri uri = data.getData();
Log.d("TAG",uri.getPath()+"");
Log.d("TAG",uri.toString());
File recoveryFIle = new File(uri.toString());
Log.d("TAG",recoveryFIle.length()+"");

No. Impossible.
Stop trying.
Moreover you need not what you call a file.

Related

Share Google File Type via Intent

I'm trying to share Google File Type (eg: application/vnd.google-apps.document, application/vnd.google-apps.form, application/vnd.google-apps.presentation, application/vnd.google-apps.spreadsheet) with the Intent.
My code:
private void shareFile(String uri) {
ContentResolver contentResolve = getContentResolver();
String mimeType = contentResolve.getType(Uri.parse(uri));
Intent intentShareFile = new Intent(Intent.ACTION_SEND);
intentShareFile.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intentShareFile.putExtra(Intent.EXTRA_STREAM,
Uri.parse(uri));
intentShareFile.setType(mimeType);
intentShareFile.putExtra(Intent.EXTRA_TEXT, "Hey, check out this file!");
startActivity(Intent.createChooser(intentShareFile, "Share File"));
}
But when I click an apps to share that file, it doesn't respond and not do anything. Even when I'm trying to upload it on Google Drive, it says that upload is unsuccessful. The share file works fine when I share pdf or docx file.
Apparently, we can't share the Google File Type via Intent. We have to convert the file that has Google File Type to something like Microsoft File Type (application/vnd.openxmlformats-officedocument.wordprocessingml.document, application/msword, application/vnd.ms-excel, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/x-excel, application/vnd.ms-powerpoint, or application/vnd.openxmlformats-officedocument.presentationml.presentation).
Is there any way to convert the URI mimeType so I can share a file that has Google File Type? For the example is changing a file that has application/vnd.google-apps.document as its mimeType to application/vnd.openxmlformats-officedocument.wordprocessingml.document, application/vnd.google-apps.presentation to application/vnd.openxmlformats-officedocument.presentationml.presentation, and etc. Is there a way to do that so I can share a file that has Google File Type? Any help will be appreciated, thanks!
Do it like this:
Intent uploadIntent = ShareCompat.IntentBuilder.from(this)
.setType(mimeType)
.setStream(fileUri)
.getIntent()
.setPackage("com.google.android.apps.docs");
startActivity(uploadIntent)
Please note setStream requires Uri.

Proper way to deal with file chosen from new Google Photos app

Not a duplicate to Choosing photo using new Google Photos app is broken
My app requests for file which can be images or videos and we only support some of the file extensions. And since MediaStore.MediaColumns.Data is deprecated, I've to go with the fileDescriptor approach to solve this. But I'm not able to figure out a way to get the extension of selected file.
The URI I get after selecting a video from the Google Photos app looks like this:
content://com.google.android.apps.photos.contentprovider/-1/2/content%3A%2F%2Fmedia%2Fexternal%2Fvideo%2Fmedia%2F25/ORIGINAL/NONE/643532680
When I try to query ContentResolver using _data column it gives an IllegalArgumentException to me, so I used below code to sort of copy the file to a temporary location.
String[] str = uri.toString().split("/");
String fileName = str[str.length - 1];
String filePath = context.getFilesDir().getAbsoluteFile() + File.separator + fileName;
File newFile = new File(filePath);
FileDescriptor fileDescriptor = context.getContentResolver().openFileDescriptor(uri, "r").getFileDescriptor();
FileInputStream fis = new FileInputStream(fileDescriptor);
copyFile(fis, newFile);
return filePath;
So, how should I get the correct mimeType of the selected file?
PS: I tried MimeTypeMap, ContentResolver.getType but none of them works.
EDIT:
Adding more details:
I'm using api level 29. Running the app on a Nexus 6(API 29) emulator. And simply calling ACTION_GET_CONTENT to allow user to select an image/video. Also included the URI I get after selecting the photo which doesn't contain mimetype(which as suggested by CommansWare is normal). When I call ContentResolver.getType on the above URI I get null.
EDIT 2:
Minimal code to reproduce:
Started an intent like this:
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.setType("video/*");
startActivityForResult(i,IMAGE_VIDEO_FETCH);
Then in the onActivityResult, checked the mimetype.
Uri media = intent.getData();
this.getContentResolver().getType(media); /// Here it returns null.

Sharing an Audio wav file

I want to share the audio file in android using but didn't found any working solution.
In My app the app records and save the audio file named as audio_1.wav each time in the app directory and then provide the option for sharing it. So always it share the file with same name(audio_1.wav).
Also checked that it is creating the file correctly.
I tried to share the audio file with below code;
File f = new File(sharePath);
Uri uri = Uri.parse(f.getAbsolutePath());
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("audio/*");
share.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(share, "Share Sound File"));
But while sharing and selecting any app it show unable to attach File.
Also Used FileProvider Class for Nougat Devices but it didn't worked as well.
Checked multiple SO Link as well:
Sharing an audio file
Picking up an audio file android
With regards to the code that you have:
Replace Uri uri = Uri.parse(f.getAbsolutePath()); with Uri uri = Uri.fromFile(f);, to get a valid Uri
Use audio/wav, not audio/*, for the MIME type
Your code will crash on Android 7.0+ with a FileUriExposedException. Use FileProvider for that, where you use <external-path> in your metadata XML resource and add the FLAG_GRANT_READ_URI_PERMISSION flag to your Intent. See the documentation for more.

Opening docx or pdf causes Android device File read error

I have created a App in which I am showing the pdf and docx file in Android Device which get download in device when button is been clicked we are displaying it in devices why using below code.
File pdfFile = new File(Environment.getExternalStorageDirectory() + "/FILE/a.docx");
Uri path = Uri.fromFile(pdfFile);
Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
pdfIntent.setDataAndType(path,"application/msword");
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(pdfIntent);
Getting an error message in Android 4.2:
file read error. File type is unsupported or the file is corrupted.
That is not the correct MIME type for DOCX files. That is the MIME type for the older DOC format. Try application/vnd.openxmlformats-officedocument.wordprocessingml.document.
Also, do not use concatenation to build file paths, as you are making assumptions about the value returned by getExternalStorageDirectory(). Use:
File pdfFile = new File(Environment.getExternalStorageDirectory(), "FILE/a.docx");

I need to open my .doc file in quickoffice in my app without looking for suitable apps (options)

I need to open my .doc file in quickoffice in my app without looking for suitable apps (options).
Here I have used this
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(file);
//String type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(
// MimeTypeMap.getFileExtensionFromUrl(uri.toString()));
//intent.setDataAndType(uri, type == null ? "*/*" : type);
intent.setDataAndType(uri, "application/msword");
startActivity((Intent.createChooser(intent,
getString(R.string.open_using))));
This Code shows suitable target apps which i dont need actually
Remove the createChooser call, and just pass the intent to startActivity. Then it will open the default app for the mime type, assuming one is set. If none is set, it may still pop up a chooser among those apps which claim they can open it. If you want to only open quick office you can do so by activity name, but then it will fail (and possibly throw an exception) if quick office is not installed.
It worked for me.
PackageManager packageManager = getPackageManager();
Intent quickOffice = packageManager
.getLaunchIntentForPackage("com.quickoffice.android");
File sdcard = Environment.getExternalStorageDirectory();
//your file location
File file = new File(sdcard, "Meta Data.doc");
Uri path = Uri.fromFile(file);
quickOffice.setAction(Intent.ACTION_VIEW);
quickOffice.setDataAndType(path, "application/msword");// ---->application/msword
startActivity(quickOffice);

Categories

Resources