Typical Scenario
In order to upload a file using WebView, it's typically needed to override WebChromeClient, and start a file chooser Activity for result:
...
webView.setWebChromeClient(new WebChromeClient() {
#Override
public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback,
FileChooserParams fileChooserParams) {
mFilePathCallback = filePathCallback;
Intent intent = new Intent(MainActivity.this, FilePickerActivity.class);
startActivityForResult(intent, 1);
return true;
}
});
...
Then, once file is selected (for simplicity, only a single file can be selected at a time), onActivityResult() is called with a file uri stored in a data object. So, the uri is retrieved and handed over to filePathCallback and the file gets uploaded:
...
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
if (requestCode == 1) {
mFilePathCallback.onReceiveValue(new Uri[] {data.getData()});
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
...
Basically, filePathCallback requires a uri to a file so that to upload it.
The problem
What if I have only an InputStream that contains the data I need to upload rather than a file with a URI? And I cannot save that data to a file so that to generate a URI (for security reasons). Is there a way to upload the data as a file in such case? In particular, can I convert an InputStream to a content URI and then upload it?
Approach 1
This approach works fine, if uri is generated using Uri.fromFile() as below:
...
Uri uri = Uri.fromFile(file);
...
Approach 2
If I implement my own ContentProvider and override openFile there in such a way, that it uses ParcelFileDescriptor.open() to create a ParcelFileDescriptor, then uploading a file based on a uri provided by getContentUri(...) is working without problems:
FileUploadProvider.java
public class FileUploadProvider extends ContentProvider {
public static Uri getContentUri(String name) {
return new Uri.Builder()
.scheme("content")
.authority(PROVIDER_AUTHORITY)
.appendEncodedPath(name)
.appendQueryParameter(OpenableColumns.DISPLAY_NAME, name)
.build();
}
#Nullable
#Override
public ParcelFileDescriptor openFile(#NonNull Uri uri, #NonNull String mode) throws FileNotFoundException {
List<String> segments = uri.getPathSegments();
File file = new File(getContext().getApplicationContext().getCacheDir(),
TextUtils.join(File.separator, segments));
return ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
}
...
}
Approach 3
However, if I create a ParcelFileDescriptor with help of ParcelFileDescriptor.createPipe(), then file upload never finishes, so it basically doesn't work:
...
public ParcelFileDescriptor openFile(#NonNull Uri uri, #NonNull String mode) throws FileNotFoundException {
List<String> segments = uri.getPathSegments();
File file = new File(getContext().getApplicationContext().getCacheDir(),
TextUtils.join(File.separator, segments));
InputStream inputStream = new FileInputStream(file);
ParcelFileDescriptor[] pipe;
try {
pipe = ParcelFileDescriptor.createPipe();
} catch (IOException e) {
throw new RuntimeException(e);
}
ParcelFileDescriptor readPart = pipe[0];
ParcelFileDescriptor writePart = pipe[1];
try {
IOUtils.copy(inputStream, new ParcelFileDescriptor.AutoCloseOutputStream(writePart));
} catch (IOException e) {
throw new RuntimeException(e);
}
return readPart;
}
...
Approach 4
To make matters worse, if I create a ParcelFileDescriptor with help of MemoryFile, then file upload never finishes as well:
...
public ParcelFileDescriptor openFile(#NonNull Uri uri, #NonNull String mode) throws FileNotFoundException {
List<String> segments = uri.getPathSegments();
File file = new File(getContext().getApplicationContext().getCacheDir(),
TextUtils.join(File.separator, segments));
try {
MemoryFile memoryFile = new MemoryFile(file.getName(), (int) file.length());
byte[] fileBytes = Files.readAllBytes(file.toPath());
memoryFile.writeBytes(fileBytes, 0, 0, (int) file.length());
Method method = memoryFile.getClass().getDeclaredMethod("getFileDescriptor");
FileDescriptor fileDescriptor = (FileDescriptor) method.invoke(memoryFile);
Constructor<ParcelFileDescriptor> constructor = ParcelFileDescriptor.class.getConstructor(FileDescriptor.class);
ParcelFileDescriptor parcelFileDescriptor = constructor.newInstance(fileDescriptor);
return parcelFileDescriptor;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
...
Why is file upload not working for Approach 3 and Approach 4?
Example
I created a sample app to showcase my issue here.
Below are steps to follow to reproduce it. First log in to your gmail account and click "create new message".
Solution
My colleagues have found a solution to this with help of StorageManager - please read more about the component here.
First, create a callback that handles file system requests from ProxyFileDescriptor in FileUploadProvider:
private static class CustomProxyFileDescriptorCallback extends ProxyFileDescriptorCallback {
private ByteArrayInputStream inputStream;
private long length;
public CustomProxyFileDescriptorCallback(File file) {
try {
byte[] fileBytes = Files.readAllBytes(file.toPath());
length = fileBytes.length;
inputStream = new ByteArrayInputStream(fileBytes);
} catch (IOException e) {
// do nothing here
}
}
#Override
public long onGetSize() {
return length;
}
#Override
public int onRead(long offset, int size, byte[] out) {
inputStream.skip(offset);
return inputStream.read(out,0, size);
}
#Override
public void onRelease() {
try {
inputStream.close();
} catch (IOException e) {
//ignore this for now
}
inputStream = null;
}
}
Then, create a file descriptor using StorageManager that will read the InputStream with help of the callback above.
...
StorageManager storageManager = (StorageManager) getContext().getSystemService(Context.STORAGE_SERVICE);
ParcelFileDescriptor descriptor;
try {
descriptor = storageManager.openProxyFileDescriptor(
ParcelFileDescriptor.MODE_READ_ONLY,
new CustomProxyFileDescriptorCallback(file),
new Handler(Looper.getMainLooper()));
} catch (IOException e) {
throw new RuntimeException(e);
}
return descriptor;
...
Please find the full code on github here.
Limitation
This approach is working only for Android API higher than 25.
Related
I'm working on an app where I'm downloading a PDF file, saving it to internal storage and then opening that file in other app using FileProvider.
Note: It may be a duplicate question, I've gone through most of the questions on StackOverflow, but still didn't find the solution.
The file is getting downloaded fine but when I'm opening it, it is empty.
The downlaoded file is 30 kb and it has 5 pages but all are empty.
Initially, I thought it is empty because the other app doesn't have permission to open the file, but I did another thing to check whether it is a permission issue. I've saved the file to external storage, still, it was empty. So, it means it is not a permission issue.
Please Note:
Along with pdf file, there is some .xls file as well and when I'm opening those in excel android app, it says cannot open the file. This indicates, that there is some issue while writing the byte stream.
Retrofit Interface.java
#GET(ApiConstants.END_POINT_DOWNLOAD_DOCUMENT)
#Streaming
Call<ResponseBody> downloadDocument(#Query("bucket") String bucket, #Query("filename") String fileName);
Code to Download the file: Here I'm checking if a file is already there, then return the file, otherwise download the file.
public LiveData<Resource<File>> openOrDownloadFile(String bucket, String fileName) {
MutableLiveData<Resource<File>> documentLiveData = new MutableLiveData<>();
documentLiveData.postValue(Resource.loading(null));
Context context = MyApp.getInstance();
final File file = new File(context.getFilesDir(), fileName);
if (file.exists()) {
documentLiveData.postValue(Resource.success(file));
} else {
Call<ResponseBody> call = apiService.downloadDocument(bucket, fileName);
call.enqueue(new Callback<ResponseBody>() {
#Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
if (response.isSuccessful()) {
appExecutors.diskIO().execute(new Runnable() {
#Override
public void run() {
try {
InputStream inputStream = null;
OutputStream outputStream = null;
try {
byte[] fileReader = new byte[4096];
inputStream = response.body().byteStream();
outputStream = new FileOutputStream(file);
while (true) {
int read = inputStream.read(fileReader);
if (read == -1) {
break;
}
outputStream.write(fileReader, 0, read);
}
documentLiveData.postValue(Resource.success(file));
outputStream.flush();
} catch (IOException e) {
documentLiveData.postValue(Resource.error("Error: Unable to save file/n"+e.getLocalizedMessage(), null));
} finally {
if (inputStream != null) {
inputStream.close();
}
if (outputStream != null) {
outputStream.close();
}
}
} catch (IOException e) {
Log.e(AppConstants.TAG, e.getMessage(), e);
documentLiveData.postValue(Resource.error("Error: Unable to save file/n"+e.getLocalizedMessage(), null));
}
}
});
} else {
documentLiveData.postValue(Resource.error("Unable to download file", null));
}
}
#Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
documentLiveData.postValue(Resource.error(t.getLocalizedMessage(), null));
}
});
}
return documentLiveData;
}
Fragment Code
private void onItemClickListener(Document document) {
mDocumentsViewModel.openORDownloadFile(document.getType(), document.getName()).observe(this, new Observer<Resource<File>>() {
#Override
public void onChanged(#Nullable Resource<File> fileResource) {
binding.setResource(fileResource);
if (fileResource.status == Status.SUCCESS) {
openFile(fileResource.data);
}
}
});
}
void openFile(File file) {
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = FileProvider.getUriForFile(getContext(), BuildConfig.APPLICATION_ID, file);
intent.setDataAndType(uri, mDocumentsViewModel.getMimeType(file.getAbsolutePath()));
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
PackageManager pm = getActivity().getPackageManager();
if (intent.resolveActivity(pm) != null) {
startActivity(intent);
} else {
Toast.makeText(getActivity(), "This file cannot be opened on this device. Please download some compatible app from play store", Toast.LENGTH_SHORT).show();
}
}
Following are the versions :
ext.retrofit_version = "2.4.0"
ext.okhttp_version = "3.8.0"
I'm struggling with this issue, it'll be a great help if you can point out the issue. Thank you.
Update: The problem was with the backend APIs. My code was correct. Once they've fixed the problem at there side, it started working at my side without any changes.
My app allows user to export its data to other users or just to save as backup.
The import/export is working FINE
In order to let the user have a sample data when it first installs my app I want to package some default data. I created the sample data, tested IS WORKING FINE, then i packaged it in assets folder and load it when user runs the app for first time.
But i'm getting file not found exception
HERE GOES THE CODE:
private List<Giveaway> loadJsonData(Uri data, User user) {
List<Giveaway> result = null;
try {
InputStream is = this.getContentResolver().openInputStream(data);
Gson parser = new GsonBuilder().setDateFormat("dd/MM/yy").setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).setLongSerializationPolicy(LongSerializationPolicy.DEFAULT).setLenient().excludeFieldsWithModifiers(Modifier.FINAL, Modifier.STATIC, Modifier.TRANSIENT).create();
Set<Giveaway> temp = new HashSet<Giveaway>(50);
temp.addAll((Collection<? extends Giveaway>) parser.fromJson(new InputStreamReader(is), TypeToken.getParameterized(List.class, Giveaway.class).getType()));
result = new ArrayList<Giveaway>(temp);
} catch (FileNotFoundException e) {
e.printStackTrace();
result = new ArrayList<Giveaway>(1);
}
return result;
}
and I call it using:
loadJsonData(Uri.parse("file:///android_asset/giveaway_export.json"), sampleUser);
file:///android_asset works for WebView and pretty much nothing else. Use AssetManager to work with assets — you get one of these by calling getAssets() on a Context, such as your Activity.
Use AssetManager this is an example:
AssetManager assetManager = getAssets();
InputStream is = null;
try {
is = assetManager.open("giveaway_export.json");
} catch (IOException e) {
e.printStackTrace();
}
so you have to change your method:
private List<Giveaway> loadJsonData(Uri data, User user) {
List<Giveaway> result = null;
try {
//InputStream is = this.getContentResolver().openInputStream(data);
AssetManager assetManager = getAssets();
InputStream is = null;
try {
is = assetManager.open("giveaway_export.json");
} catch (IOException e) {
e.printStackTrace();
}
Gson parser = new GsonBuilder().setDateFormat("dd/MM/yy").setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).setLongSerializationPolicy(LongSerializationPolicy.DEFAULT).setLenient().excludeFieldsWithModifiers(Modifier.FINAL, Modifier.STATIC, Modifier.TRANSIENT).create();
Set<Giveaway> temp = new HashSet<Giveaway>(50);
temp.addAll((Collection<? extends Giveaway>) parser.fromJson(new InputStreamReader(is), TypeToken.getParameterized(List.class, Giveaway.class).getType()));
result = new ArrayList<Giveaway>(temp);
} catch (FileNotFoundException e) {
e.printStackTrace();
result = new ArrayList<Giveaway>(1);
}
return result;
}
Remember if you are using android 6.0+ you need to declared the permission:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
and require manually permissions:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
//Verify permission for Android 6.0+
checkExternalStoragePermission();
}
use this method:
private void checkExternalStoragePermission() {
int permissionCheck = ContextCompat.checkSelfPermission(
this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
Log.i("Message", "You require permissions!.");
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 225);
} else {
Log.i("Message", "you have already permissions!");
}
}
I have uploaded a Pdf file to firebase storage, after uploading the pdf file to firebase storage, I am getting the download url. Now I want to open the pdf file from this download url in my application.
Below is the url I am getting after uploading the pdf file to firebase storage.
https://firebasestorage.googleapis.com/v0/b/realtime-chat-46f4c.appspot.com/o/documents%2Fbf307aa5-79ae-4532-8128-ee394537b357.pdf?alt=media&token=2d0c5329-4717-4adc-9418-6614913e5bfa
Now I want to open an intent to view this pdf file, I've used the below code for this :
String url = "https://firebasestorage.googleapis.com/v0/b/realtime-chat-46f4c.appspot.com/o/documents%2Fbf307aa5-79ae-4532-8128-ee394537b357.pdf?alt=media&token=2d0c5329-4717-4adc-9418-6614913e5bfa";
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(url), "application/pdf");
startActivity(Intent.createChooser(intent, "Choose an Application:"));
My phone is having applications which can open this pdf file, still it says there are no applications installed to view this file.
If I convert this url in File and use the below code, then the chooser of applications get opened but it gives error that the file can't be opened.
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
File file = new File(name);
intent.setDataAndType(Uri.fromFile(file), "aplication/pdf");
I've seen many answers that says first download the file and then open it, but I don't want to download the file, I just want to view it.
Please help me if anyone have any idea about this.
Thanks a lot in advanced.
You should use the Intent Chooser
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(url), "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Intent newIntent = Intent.createChooser(intent, "Open File");
try {
startActivity(newIntent);
} catch (ActivityNotFoundException e) {
// Instruct the user to install a PDF reader here, or something
}
Your phone is not detecting the PDF because the URL you are using is a String object and not a PDF object. Technically, it should never open from the phone as PDF unless you download the PDF. PDF viewers on a phone only get activated once the Android OS tells them that it has a valid PDF file. Please take a look at source code of any of the PDF libraries to understand this better -> https://android-arsenal.com/tag/72?sort=created
However, to answer your problem, you should try opening your URL as a URL. It will activate the browser on your phone which in turn will detect that it needs a PDF reader and will activate the appropriate reader.
Something like
String url = "https://firebasestorage.googleapis.com/v0/b/realtime-chat-46f4c.appspot.com/o/documents%2Fbf307aa5-79ae-4532-8128-ee394537b357.pdf?alt=media&token=2d0c5329-4717-4adc-9418-6614913e5bfa";
public void openWebPage(String url) {
Uri webpage = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW, webpage);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
However, I am thinking that you want to just validate if the file got uploaded successfully. And if there is a local copy on the phone then just open the local copy instead of downloading another copy. If that is the case, then I recommend using file metadata to achieve that. Something like:
// Create file metadata including the content type
StorageMetadata metadata = new StorageMetadata.Builder()
.setCustomMetadata("myPDFfile", "localPDFfileName")
.build();
// Upload the file and metadata
uploadTask = storageRef.child("pdf/localPDFfileName.pdf").putFile(file, metadata);
And on your success listener, retrieve the file metadata. If it is downloaded and looks complete, then open the PDF from your local phone storage. If not, then try downloading from the downloadURL as I mention at the start of the post. That should cover the validations you are trying to do.
Do elaborate more if I did not understand your problem, correctly. I will pick it up and amend my reply accordingly.
Please add following files/method to your project
PdfDownloader.java
PDFDownloaderAsyncTask.java
and then using following method handleViewPdf to view pdf:
private void handleViewPdf () {
File folder = getAppDirectory(context);
String fileName = "test.pdf";// getPdfFileName(pdfUrl);
File pdfFile = new File(folder, fileName);
if (pdfFile.exists () && pdfFile.length () > 0) {
openPDFFile (context, Uri.fromFile(pdfFile));
}
else {
if (pdfFile.length () == 0) {
pdfFile.delete ();
}
try {
pdfFile.createNewFile ();
}
catch (IOException e) {
e.printStackTrace ();
}
ArrayList<String> fileNameAndURL = new ArrayList<> ();
fileNameAndURL.add (pdfFile.toString ());
fileNameAndURL.add (pdfUrl);
fileNameAndURL.add (fileName);
if (pdfDownloaderAsyncTask == null) {
pdfDownloaderAsyncTask = new PDFDownloaderAsyncTask (context, pdfFile);
}
if (hasInternetConnection (context)) {
if (!pdfDownloaderAsyncTask.isDownloadingPdf ()) {
pdfDownloaderAsyncTask = new PDFDownloaderAsyncTask (context, pdfFile);
pdfDownloaderAsyncTask.execute (fileNameAndURL);
}
}
else {
//show error
}
}
}
PDFDownloaderAsyncTask.java
import java.io.File;
import java.util.ArrayList;
import android.content.Context;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Handler;
import android.text.TextUtils;
import android.widget.Toast;
public class PDFDownloaderAsyncTask extends AsyncTask<ArrayList<String>, Void, String> {
private boolean isDownloadingPdf = false;
private File file;
private Context context;
public PDFDownloaderAsyncTask (Context context, File file) {
this.file = file;
this.context = context;
this.isDownloadingPdf = false;
}
public boolean isDownloadingPdf () {
return this.isDownloadingPdf;
}
#Override
protected void onPreExecute () {
super.onPreExecute ();
//show loader etc
}
#Override
protected String doInBackground (ArrayList<String>... params) {
isDownloadingPdf = true;
File file = new File (params[0].get (0));
String fileStatus = PdfDownloader.downloadFile (params[0].get (1), file);
return fileStatus;
}
#Override
protected void onPostExecute (String result) {
super.onPostExecute (result);
Loader.hideLoader ();
if (!TextUtils.isEmpty (result) && result.equalsIgnoreCase (context.getString (R.string.txt_success))) {
showPdf ();
}
else {
isDownloadingPdf = false;
Toast.makeText (context, context.getString (R.string.error_could_not_download_pdf), Toast.LENGTH_LONG).show ();
file.delete ();
}
}
#Override
protected void onCancelled () {
isDownloadingPdf = false;
super.onCancelled ();
//Loader.hideLoader ();
}
#Override
protected void onCancelled (String s) {
isDownloadingPdf = false;
super.onCancelled (s);
//Loader.hideLoader ();
}
private void showPdf () {
new Handler ().postDelayed (new Runnable () {
#Override
public void run () {
isDownloadingPdf = false;
openPDFFile (context, Uri.fromFile (file));
}
}, 1000);
}
}
PdfDownloader.java
package com.pdf;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class PdfDownloader {
private static final int MEGABYTE = 1024 * 1024;
public static String downloadFile (String fileUrl, File directory) {
String downloadStatus;
try {
URL url = new URL (fileUrl);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection ();
urlConnection.connect ();
InputStream inputStream = urlConnection.getInputStream ();
FileOutputStream fileOutputStream = new FileOutputStream (directory);
int totalSize = urlConnection.getContentLength ();
Log.d ("PDF", "Total size: " + totalSize);
byte[] buffer = new byte[MEGABYTE];
int bufferLength = 0;
while ((bufferLength = inputStream.read (buffer)) > 0) {
fileOutputStream.write (buffer, 0, bufferLength);
}
downloadStatus = "success";
fileOutputStream.close ();
}
catch (FileNotFoundException e) {
downloadStatus = "FileNotFoundException";
e.printStackTrace ();
}
catch (MalformedURLException e) {
downloadStatus = "MalformedURLException";
e.printStackTrace ();
}
catch (IOException e) {
downloadStatus = "IOException";
e.printStackTrace ();
}
Log.d ("PDF", "Download Status: " + downloadStatus);
return downloadStatus;
}
public static void openPDFFile (Context context, Uri path) {
Intent intent = new Intent (Intent.ACTION_VIEW);
intent.setDataAndType (path, "application/pdf");
intent.setFlags (Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
context.startActivity (intent);
}
catch (ActivityNotFoundException e) {
Toast.makeText (context, context.getString (R.string.txt_no_pdf_available), Toast.LENGTH_SHORT).show ();
}
Loader.hideLoader ();
}
public static File getAppDirectory (Context context) {
String extStorageDirectory = Environment.getExternalStorageDirectory ().toString ();
File folder = new File (extStorageDirectory, context.getString (R.string.app_folder_name).trim ());
if (!folder.exists ()) {
boolean success = folder.mkdirs();
Log.d ("Directory", "mkdirs():" + success);
}
return folder;
}
}
if you want to open the saved pdf file in your application rather than creating a intent to open in other application, you can use the following library to create the PdfView in Xml file. AndroidPdfViewer (this library also provides the zoom in-out facility)
After that as mentioned in the Firebase Document use the saved path reference of the file location in firebase Storage to fetch the file in Bytes and add it to your PdfView as
For example your file path will be like : "/folder1/yourfile.pdf"
pdfView = (PDFView) findViewById(R.id.pdfView);
mFirebaseStorage=FirebaseStorage.getInstance().getReference();
final long ONE_MEGABYTE = 1024 * 1024;
mFirebaseStorage.child("YourSavedFilePathRef").getBytes(ONE_MEGABYTE).addOnSuccessListener(new OnSuccessListener<byte[]>() {
#Override
public void onSuccess(byte[] bytes) {
pdfView.fromBytes(bytes).load();
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Toast.makeText(MainActivity.this,"download unsuccessful",Toast.LENGTH_LONG).show();
}
});
Simple code :)
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Uri fileuri = Uri.parse("URL of file on storage") ;
intent.setDataAndType(fileuri,"application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Intent in = Intent.createChooser(intent,"open file");
in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(in);
Hope this works :)
try to encode your firebase database file url. it work with me
Use Webview for view pdf file-
WebView webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
String pdf = "https://firebasestorage.googleapis.com/v0/b/realtime-chat-46f4c.appspot.com/o/documents%2Fbf307aa5-79ae-4532-8128-ee394537b357.pdf?alt=media&token=2d0c5329-4717-4adc-9418-6614913e5bfa";
webview.loadUrl("http://drive.google.com/viewerng/viewer?embedded=true&url=" + pdf);
I've followed the tutorial on the cloud print website and created a Print activity by copy and pasting the example code.
I'm trying to print an image from the MediaStore but when I get as far as the print screen nothing happens after I press the 'Print' button.
This is the code I'm using to call the intent
Intent printIntent = new Intent(GalleryActivity.this, PrintDialogActivity.class);
Uri fileUri = Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, Long.toString(imageId));
Log.d(this, "File Uri:" + fileUri);
printIntent.setDataAndType(fileUri, "image/*");
startActivity(printIntent);
The Uri being logged looks like content://media/external/images/media/26848
The Logcat output when I press the print button is
[INFO:CONSOLE(1)] "Uncaught TypeError: Object [object Object] has no method 'getType'", source: https://www.google.com/cloudprint/dialog.html (1)
[INFO:CONSOLE(280)] "Uncaught TypeError: Cannot call method 'k' of null", source: https://www.google.com/cloudprint/client/442365700-dialog_mobile.js (280)
Edit: I've tested on a couple of other devices and I don't get the above log output, so it may not be related. However, the result is the same on every device; when I press the print button in the webview nothing happens.
Add the #JavascriptInterface in the methods of PrintDialogJavaScriptInterface class.
final class PrintDialogJavaScriptInterface {
#JavascriptInterface
public String getType() {
return cloudPrintIntent.getType();
}
#JavascriptInterface
public String getTitle() {
return cloudPrintIntent.getExtras().getString("title");
}
#JavascriptInterface
public String getContent() {
try {
ContentResolver contentResolver = getContentResolver();
InputStream is = contentResolver.openInputStream(cloudPrintIntent.getData());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[4096];
int n = is.read(buffer);
while (n >= 0) {
baos.write(buffer, 0, n);
n = is.read(buffer);
}
is.close();
baos.flush();
return Base64.encodeToString(baos.toByteArray(), Base64.DEFAULT);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
#JavascriptInterface
public String getEncoding() {
return CONTENT_TRANSFER_ENCODING;
}
#JavascriptInterface
public void onPostMessage(String message) {
if (message.startsWith(CLOSE_POST_MESSAGE_NAME)) {
finish();
}
}
}
I am trying a sample program to store a file in the internal storage and the open it using
Intent.ACTION_VIEW.
For storing the file in private mode I followed the steps provided here.
I was able to find the created file in the internal storage at /data/data/com.storeInternal.poc/files .*
But when I tried to open file,it does not open.
Please find below the code I used for it.
public class InternalStoragePOCActivity extends Activity {
/** Called when the activity is first created. */
String FILENAME = "hello_file.txt";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
createFile();
openFile(FILENAME);
}
public FileOutputStream getStream(String path) throws FileNotFoundException {
return openFileOutput(path, Context.MODE_PRIVATE);
}
public void createFile(){
String string = "hello world!";
FileOutputStream fout = null;
try {
//getting output stream
fout = getStream(FILENAME);
//writng data
fout.write(string.getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(fout!=null){
//closing the output stream
try {
fout.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public void openFile(String filePath) {
try {
File temp_file = new File(filePath);
Uri data = Uri.fromFile(temp_file);
String type = getMimeType(data.toString());
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(data, type);
startActivity(intent);
} catch (Exception e) {
Log.d("Internal Storage POC ", "No Supported Application found to open this file");
e.printStackTrace();
}
}
public static String getMimeType(String url) {
String type = null;
String extension = MimeTypeMap.getFileExtensionFromUrl(url);
if (extension != null) {
MimeTypeMap mime = MimeTypeMap.getSingleton();
type = mime.getMimeTypeFromExtension(extension);
}
return type;
}
}
How may I open the file stored using Context.MODE_PRIVATE by any other existing/appropriate app. Eg: file.pdf should be opened by PDF reader,Video by video Players,etc.
You can't share/send a file in internal storage, as referenced by a URI, via an Intent to another app. An app cannot read another app's private data (unless it's through a Content Provider). You pass the URI of the file in the intent (not the actual file itself) and the app that receives the intent needs to be able to read from that URI.
The simplest solution is to copy the file to external storage first and share it from there. If you don't want to do that, create a Content Provider to expose your file. An example can be found here: Create and Share a File from Internal Storage
EDIT: To use a content provider:
First create your content provider class as below. The important override here is 'openFile'. When your content provider is called with a file URI, this method will run and return a ParcelFileDescriptor for it. The other methods need to be present as well since they are abstract in ContentProvider.
public class MyProvider extends ContentProvider {
#Override
public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
File privateFile = new File(getContext().getFilesDir(), uri.getPath());
return ParcelFileDescriptor.open(privateFile, ParcelFileDescriptor.MODE_READ_ONLY);
}
#Override
public int delete(Uri arg0, String arg1, String[] arg2) {
return 0;
}
#Override
public String getType(Uri arg0) {
return null;
}
#Override
public Uri insert(Uri arg0, ContentValues arg1) {
return null;
}
#Override
public boolean onCreate() {
return false;
}
#Override
public Cursor query(Uri arg0, String[] arg1, String arg2, String[] arg3,
String arg4) {
return null;
}
#Override
public int update(Uri arg0, ContentValues arg1, String arg2, String[] arg3) {
return 0;
}
}
Define your provider in the Manifest within the application tag, with exported=true to allow other apps to use it:
<provider android:name=".MyProvider" android:authorities="your.package.name" android:exported="true" />
In the openFile() method you have in your Activity, set up a URI as below. This URI points to the content provider in your package along with the filename:
Uri uri = Uri.parse("content://your.package.name/" + filePath);
Finally, set this uri in the intent:
intent.setDataAndType(uri, type);
Remember to insert your own package name where I have used 'your.package.name' above.
If anyone still faces an issue like: the file couldn't be accessed. This might help u. Along with the content provider u have to set the following flag too...
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
or
intent.setFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()
+ "Foder path" + "/" + "File Name");
Uri uri = Uri.fromFile(file);
Intent in = new Intent(Intent.ACTION_VIEW);
in.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
in.setDataAndType(uri, "mime type");
startActivity(in);
[Note : Please select mime type as per your file type]