I have an Android app that at some point, needs to create and delete some text files on Google Drive as well as download / grab the content of those files to display it in an activity.
So I've been trying for some time to find a way to do this using only the file's name but I seem to be having a lot of problem finding some info on how to do it. Moreover, not being a Java dev does not make things easier.
I managed to create a file inside the root folder:
private void createFile()
{
println("CreateFileActivity > createFile");
final Task<DriveFolder> rootFolderTask = getDriveResourceClient().getRootFolder();
final Task<DriveContents> createContentsTask = getDriveResourceClient().createContents();
Tasks.whenAll(rootFolderTask, createContentsTask)
.continueWithTask(task -> {
DriveFolder parent = rootFolderTask.getResult();
DriveContents contents = createContentsTask.getResult();
OutputStream outputStream = contents.getOutputStream();
try (Writer writer = new OutputStreamWriter(outputStream))
{
writer.write("SOME_TEXT_HERE");
}
MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
.setTitle("MyFile.txt")
.setMimeType("text/plain")
.setStarred(true)
.build();
return getDriveResourceClient().createFile(parent, changeSet, contents);
})
.addOnSuccessListener(this,
driveFile -> {
System.out.println("File created");
Intent resultActvityIntent = new Intent(getApplicationContext(), ResultActivity.class);
startActivity(resultActvityIntent);
})
.addOnFailureListener(this, e -> {
Toast.makeText(this, "Unable to create file", Toast.LENGTH_SHORT).show();
System.out.println("Unable to create file");
Log.e(TAG, "Unable to create file", e);
Intent resultActvityIntent = new Intent(getApplicationContext(), ResultActivity.class);
startActivity(resultActvityIntent);
});
}
To my surprise however, it creates a new file with the same name every time instead of overwriting it.
Also, I cannot seem to be able to delete the file or download it / grad the content using only the file name.
I found a lot of info on how to delete the file using the file ID and I also found an example provided by Google but it's not really what I need.
#Override
protected void onDriveClientReady()
{
pickTextFile()
.addOnSuccessListener(this,
driveId -> deleteFile(driveId.asDriveFile()))
.addOnFailureListener(this, e -> {
Log.e(TAG, "No file selected", e);
showMessage(getString(R.string.file_not_selected));
finish();
});
}
private void deleteFile(DriveFile file)
{
// [START delete_file]
getDriveResourceClient()
.delete(file)
.addOnSuccessListener(this,
aVoid -> {
showMessage(getString(R.string.file_deleted));
finish();
})
.addOnFailureListener(this, e -> {
Log.e(TAG, "Unable to delete file", e);
showMessage(getString(R.string.delete_failed));
finish();
});
// [END delete_file]
}
Any ideas on how to do this or where to start looking?
Or it's not possible to delete the file directly from within an app?
This is how the Google drive API works. Everything uses the file id. What you should be doing is a file.list sending the q parameters to search for files with the correct name and file type. You will then have the file id to be able to update the file.
Google drive API doesn't prevent you from creating more than one file with the same name.
Following #DalmTo's suggestion, here's my solution for deleting a file on Google Drive. The example below skips trash and deletes the file permanently.
private static final String fileName = "MyAppsTextFile.txt";
private void deleteExistingFile()
{
println("DeleteFileActivity > deleteExistingFile");
Query query = new Query.Builder()
.addFilter(Filters.eq(SearchableField.TITLE, fileName))
.build();
Task<MetadataBuffer> queryTask = getDriveResourceClient().query(query);
queryTask.addOnSuccessListener( this,
new OnSuccessListener<MetadataBuffer>()
{
#Override
public void onSuccess(MetadataBuffer metadataBuffer)
{
System.out.println("Success. File/s found!");
for(Metadata m : metadataBuffer)
{
DriveResource driveResource = m.getDriveId().asDriveResource();
System.out.println("Deleting file " + fileName + " with DriveID m.getDriveId()");
getDriveResourceClient().delete(driveResource);
}
}
})
.addOnFailureListener(this, new OnFailureListener()
{
#Override
public void onFailure(#NonNull Exception e)
{
System.out.println("ERROR: File not found!");
}
});
}
And since the thread title is Download and Delete, here's the code to get the file content from Google drive:
private static final String fileName = "MyAppsTextFile.txt";
private void getFiles()
{
System.out.println("GetGoogleDriveFile > getFiles");
Query query = new Query.Builder()
.addFilter(Filters.eq(SearchableField.TITLE, fileName))
.build();
Task<MetadataBuffer> queryTask = getDriveResourceClient().query(query);
queryTask
.addOnSuccessListener(this,
new OnSuccessListener<MetadataBuffer>()
{
#Override
public void onSuccess(MetadataBuffer metadataBuffer)
{
System.out.println("On SUCCESS");
for( Metadata m : metadataBuffer )
{
DriveFile driveFile = m.getDriveId().asDriveFile();
getFileContents(driveFile);
}
}
})
.addOnFailureListener(this, new OnFailureListener()
{
#Override
public void onFailure(#NonNull Exception e)
{
System.out.println("On FAILURE");
}
});
}
private void getFileContents(DriveFile myFile)
{
System.out.println("GetGoogleDriveFile > getFileContents");
Task<DriveContents> openFileTask =
getDriveResourceClient().openFile(myFile, DriveFile.MODE_READ_ONLY);
openFileTask
.continueWithTask(new Continuation<DriveContents, Task<Void>>()
{
#Override
public Task<Void> then(#NonNull Task<DriveContents> task) throws Exception
{
DriveContents contents = task.getResult();
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(contents.getInputStream())))
{
StringBuilder builder = new StringBuilder();
String line;
while ((line = reader.readLine()) != null)
{
builder.append(line).append("\n");
}
userData = builder.toString();
}
System.out.println("We have the file content!");
Task<Void> discardTask = getDriveResourceClient().discardContents(contents);
return discardTask;
}
})
.addOnFailureListener(new OnFailureListener()
{
#Override
public void onFailure(#NonNull Exception e)
{
System.out.println("Unable to read file!");
}
});
}
I'm pretty sure this can be improved but I guess it's a start for anyone looking for a solution.
Related
So what i want is to delete file using URI
Do not flag is duplicate
I tried many answer but nothing worked here is the question i tried
How to delete file that is created using Uri?
Delete file using Uri
How to delete file that is created using Uri?
I write code to get images from gallery and then copy it to directory called ".blackhat" and delete original one (Kind of Move File Function)..But it's not working. even it is not generation Log so i can check the error.
this code working proper for copying file but not deleting after copying....
if(requestCode == 2 && data.getData() !=null){
if(cd == null){
path.add(data.getData());
Random rn = new Random();
if(copyFileFromUri(this,data.getData(),String.valueOf(rn.nextInt(500)))){
File fdelete = new File(data.getData().getPath());
if (fdelete.exists()) {
if (fdelete.delete()) {
Log.d("delete","deleted");
} else {
Log.d("delete","not deleted");
}
}
Toast.makeText(this, "Done", Toast.LENGTH_SHORT).show();
}
}else{
for(int i=0;i<data.getClipData().getItemCount();i++){
path.add(data.getClipData().getItemAt(i).getUri());
Log.d("RjList",path.get(i).toString());
Random rn=new Random();
if(copyFileFromUri(this,data.getClipData().getItemAt(i).getUri(),String.valueOf(rn.nextInt(500)))){
File fdelete = new File(data.getClipData().getItemAt(i).getUri().getPath());
if (fdelete.exists()) {
if (fdelete.delete()) {
Log.d("delete","deleted");
} else {
Log.d("delete","not deleted");
}
}
Toast.makeText(this, "Done", Toast.LENGTH_SHORT).show();
}
}
}
}
Where am i doing wrong ????
Thank in advance...
public static boolean delete(final Context context, final File file) {
final String pathone = MediaStore.MediaColumns.DATA + "=?";
final String[] selectedArgs = new String[] {
file.getAbsolutePath()
};
final ContentResolver contentResolver = context.getContentResolver();
final Uri fileUri = MediaStore.Files.getContentUri("external");
contentResolver.delete(fileUri, pathone, selectedArgs );
if (file.exists()) {
contentResolver.delete(fileUri, pathone, selectedArgs );
}
return !file.exists();
}
File fdelete = new File(data.getData().getPath(),"here you should pass the file name");
if (fdelete.exists()) {
if (fdelete.delete()) {
Log.d("delete","deleted");
} else {
Log.d("delete","not deleted");
}
}
I'm using google API v3 for check exist folder. If folder does not exist, then create the new folder. This is my code for creating folder
private void createFolderInDrive() throws IOException {
boolean existed = checkExistedFolder("MyFolder");
if (existed = false) {
File fileMetadata = new File();
fileMetadata.setName("MyFolder");
fileMetadata.setMimeType("application/vnd.google-apps.folder");
File file = mService.files().create(fileMetadata)
.setFields("id")
.execute();
System.out.println("Folder ID: " + file.getId());
Log.e(this.toString(), "Folder Created with ID:" + file.getId());
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(),
"Folder is existed already", Toast.LENGTH_SHORT).show();
}
}
and here is the code for checking exist file
private boolean checkExistedFolder(String folderName) {
//File file = null;
boolean existedFolder = true;
// check if the folder exists already
try {
//String query = "mimeType='application/vnd.google-apps.folder' and trashed=false and title='" + "Evacuation Kit" + "'";
String query = "mimeType='application/vnd.google-apps.folder' and trashed=false and name='Evacuation Kit'";
// add parent param to the query if needed
//if (parentId != null) {
//query = query + " and '" + parentId + "' in parents";
// }
Drive.Files.List request = mService.files().list().setQ(query);
FileList fileList = request.execute();
if (fileList.getFiles().size() == 0 ) {
// file = fileList.getFiles().get(0);
existedFolder = false;
}
} catch (IOException e) {
e.printStackTrace();
}
return existedFolder;
fileList.getFiles().size() keep returning 3, even there is no folder on g drive. Can you guys tell me where am I doing wrong?
In the code you show, checkExistedFolder is always looking for the name "Evacuation Kit" and not using the argument folderName. Maybe this is the main reason you're always getting 3 from fileList.getFiles().size().
Also there's an assignment in if (existed = false), you should use if ( false == existed ) -using the static value in the left side of the comparison helps avoiding such mistakes-, or if (!existed). Note that it's important to check the nextPageToken when calling Files:list to check if there is more pages to look for the file. See more here https://developers.google.com/drive/api/v3/reference/files/list and Create folder if it does not exist in the Google Drive
This code will check if folder exist on drive. if exists, it will return id else create folder and returns id.
private DriveFile file;
GoogleApiClient mGoogleApiClient;
#Override
public void onConnected(#Nullable Bundle bundle) {
Log.e(TAG, "connected");
new Thread(new Runnable() {
#Override
public void run() {
DriveId Id = getFolder(Drive.DriveApi.getRootFolder(mGoogleApiClient).getDriveId(), "FOLDER_NAME");
Log.e(TAG, "run: " + Id);
}
}).start();
}
DriveId getFolder(DriveId parentId, String titl) {
DriveId dId = null;
if (parentId != null && titl != null) try {
ArrayList<Filter> fltrs = new ArrayList<>();
fltrs.add(Filters.in(SearchableField.PARENTS, parentId));
fltrs.add(Filters.eq(SearchableField.TITLE, titl));
fltrs.add(Filters.eq(SearchableField.MIME_TYPE, DriveFolder.MIME_TYPE));
Query qry = new Query.Builder().addFilter(Filters.and(fltrs)).build();
MetadataBuffer mdb = null;
DriveApi.MetadataBufferResult rslt = Drive.DriveApi.query(mGoogleApiClient, qry).await();
if (rslt.getStatus().isSuccess()) try {
mdb = rslt.getMetadataBuffer();
if (mdb.getCount() > 0)
dId = mdb.get(0).getDriveId();
} catch (Exception ignore) {
} finally {
if (mdb != null) mdb.close();
}
if (dId == null) {
MetadataChangeSet meta = new MetadataChangeSet.Builder().setTitle(titl).setMimeType(DriveFolder.MIME_TYPE).build();
DriveFolder.DriveFolderResult r1 = parentId.asDriveFolder().createFolder(mGoogleApiClient, meta).await();
DriveFolder dFld = (r1 != null) && r1.getStatus().isSuccess() ? r1.getDriveFolder() : null;
if (dFld != null) {
DriveResource.MetadataResult r2 = dFld.getMetadata(mGoogleApiClient).await();
if ((r2 != null) && r2.getStatus().isSuccess()) {
dId = r2.getMetadata().getDriveId();
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return dId;
}
The code working for me with updated API on Kotlin:
override fun createFolder(name: String): Task<GoogleDriveFileHolder> {
check(googleDriveService != null) { "You have to init Google Drive Service first!" }
check(search(name, FOLDER_MIME_TYPE).not()){"folder already exist"}
return Tasks.call<GoogleDriveFileHolder>(
mExecutor,
Callable<GoogleDriveFileHolder> {
val metadata = File()
.setMimeType(FOLDER_MIME_TYPE)
.setName(name)
GoogleDriveFileHolder(
googleDriveService!!.files()
.create(metadata)
.setFields("id")
.execute() ?: throw IOException("Null result when requesting file creation.")
)
})
}
private fun search(name: String, mimeType:String): Boolean {
var pageToken: String? = null
do {
val result: FileList =
googleDriveService!!
.files()
.list()
.setQ("mimeType='$FOLDER_MIME_TYPE'")
.setSpaces("drive")
.setFields("nextPageToken, files(id, name)")
.setPageToken(pageToken)
.execute()
for (file in result.files) {
Log.d(TAG_UPLOAD_FILE , "Found file: %s (%s)\n ${file.name}, ${file.id} ")
if (name == file.name) return true
}
pageToken = result.nextPageToken
} while (pageToken != null)
return false
}
private const val FOLDER_MIME_TYPE= "application/vnd.google-apps.folder"
I'm making an android application, which saves data to a file in the settings activity.
I made some custom functions to ease writing my files, they're in a class all my activities inherit from, including the settings activity.
Custom functions:
public void WriteToFile(String filename, String tag, String value) {
try {
FileOutputStream fileOut = openFileOutput(filename + ".txt", MODE_PRIVATE);
OutputStreamWriter writer = new OutputStreamWriter(fileOut);
writer.write(ReadFile(filename + ".txt") + tag + ":" + value + ";");
writer.close();
} catch (Exception e) {
Toast.makeText(this, "ERROR: " + e.toString(), Toast.LENGTH_LONG).show();
Log.e("Error writing: ", e.toString());
}
}
public void WipeFile(String filename) {
try {
FileOutputStream fileOut = openFileOutput(filename + ".txt", MODE_PRIVATE);
OutputStreamWriter writer = new OutputStreamWriter(fileOut);
writer.write("");
writer.close();
} catch (Exception e) {
Toast.makeText(this, "ERROR: " + e.toString(), Toast.LENGTH_LONG).show();
Log.e("Error writing: ", e.toString());
}
}
public String ReadFile(String filename) {
try {
FileInputStream fileIn = openFileInput(filename + ".txt");
InputStreamReader InputRead = new InputStreamReader(fileIn);
char[] inputBuffer = new char[10000];
String content = "", readString;
int charRead;
while ((charRead = InputRead.read(inputBuffer)) > 0) {
readString = String.copyValueOf(inputBuffer, 0, charRead);
content += readString;
}
InputRead.close();
return content;
} catch (Exception e) { WipeFile(filename); return ""; }
}
public String FileValue(String filename, String tag, String defaultValue) {
String[] content = ReadFile(filename + ".txt").split(";");
for (String pair : content) {
if (pair.split(":")[0].equals(tag)) return pair.split(":")[1];
} WriteToFile(filename, tag, defaultValue); return defaultValue;
}
Settings activity:
#Override
#SuppressWarnings("ConstantConditions")
protected void onCreate(Bundle savedInstanceState) {
ToolbarTitle = "Settings";
ActivityID = R.layout.activity_settings;
ToolbarID = R.id.settings_toolbar;
ToolbarIcon = R.mipmap.settings_icon;
ActivityLayout = R.id.settings_layout;
super.onCreate(savedInstanceState);
if (prefs.getInt("LoggedinID", 0) == 0) findViewById(R.id.settings_user).setVisibility(View.GONE);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.settings_lowBattery, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
int spinnerPosition = adapter.getPosition(FileValue("settings", "Alert", "20%"));
Spinner battery = ((Spinner) findViewById(R.id.settings_battery));
battery.setAdapter(adapter);
battery.setSelection(spinnerPosition);
((Switch) findViewById(R.id.settings_notifications)).setChecked(FileValue("settings", "Notifications", "1").equals("1"));
findViewById(R.id.settings_ads).setVisibility((FileValue("settings", "Ads", "1").equals("1") ? View.VISIBLE : View.INVISIBLE));
}
#SuppressWarnings("ConstantConditions")
public void Apply(View view) {
WipeFile("settings");
WriteToFile("settings", "Notifications", (((Switch) findViewById(R.id.settings_notifications)).isChecked() ? "1" : "0"));
WriteToFile("settings", "Alert", ((Spinner) findViewById(R.id.settings_battery)).getSelectedItem().toString());
}
public void Ads(View view) {
Toast.makeText(this, "Just a prank, bro", Toast.LENGTH_SHORT).show();
WriteToFile("settings", "Ads", "0");
}
What's weird is that it all worked when it was messy and not in custom functions, any idea why?
The problem seems to occur in the ReadFile function, where InputRead.read(inputBuffer) returns -1 (No data in file).
I have no idea how to even check where the problem lies, when writing to the file or when reading from it....
Thanks ahead
PROBLEM SOLVED
1. The ReadFile function that was inside the writer.write function couldn't open the file and read it since the writer kept it open for itself.
2. That same ReadFile function was provided with (filename + ".txt"), and added ".txt" to it as well.
It seems like the problem might be in how you are appending to file...
Internally your write function opens the file, then before closing it, your read function opens the same file and closes it. It could be that either the read function is failing when it tries to open the file because it is already open, but not closed. OR it could be that when the read function closes the file it also closes the file for the write function...
So the problem seems to be that you want to append to the file in your write function, but you are implementing it poorly. You do not need to rewrite the contents to file. You just need to find the proper flag to open the file for appending.
You should use a java.util.Properties for your settings. It is like a Map<String, String>.
To load all your settings, use load(Reader reader).
To save all your settings, use save(OutputStream out, String comments).
I have a code which is checking a defined type of audio file in folder and calling converter to change its format. Now when first file is passed, converter is called and as file is in process of being conversion, for loop called converter again for second file. In this i felt earlier/later process is terminated and hence i m getting only file converted as output. Code is here. How can i manage to get all files convereted.
public void convertAudio(View v) {
final File pathanme = new File(Environment.getExternalStorageDirectory() + "/sdcard/test");
File files[] = pathanme.listFiles();
for (File f : files) {
if (f.getName().endsWith(".mp4")) {
String filename = f.getName().toLowerCase().toString();
System.out.println(filename);
File wavFile = new File(pathanme, filename);
IConvertCallback callback = new IConvertCallback() {
#Override
public void onSuccess(File convertedFile) {
Toast.makeText(NewMainActivity.this, "SUCCESS: " + convertedFile.getPath(), Toast.LENGTH_LONG).show();
}
#Override
public void onFailure(Exception error) {
Toast.makeText(NewMainActivity.this, "ERROR: " + error.getMessage(), Toast.LENGTH_LONG).show();
}
};
Toast.makeText(this, "Converting audio file..." + filename, Toast.LENGTH_SHORT).show();
AndroidAudioConverter.with(this)
.setFile(wavFile)
.setFormat(AudioFormat.MP3)
.setCallback(callback)
.convert();
}
}
If u see there is success message against conversion and i never got this under for loop whereas if i pass only one file, i got success message. pls advice.
You could add a class instance variable for an index and increment it as necessary, calling the convert() method recursively as necessary. It'd look something like this (Java is a little rusty, you may have to clean up syntax):
public class MyClass {
private int fileIndex = 0;
private File[] files;
public void convertAudio(View v) {
final File pathanme = new File(Environment.getExternalStorageDirectory() + "/sdcard/test");
this.files = pathanme.listFiles();
fileIndex = 0;
convertFile(files[fileIndex]);
}
private void convertFile(File f) {
if (f.getName().endsWith(".mp4")) {
String filename = f.getName().toLowerCase().toString();
System.out.println(filename);
File wavFile = new File(pathanme, filename);
IConvertCallback callback = new IConvertCallback() {
#Override
public void onSuccess(File convertedFile) {
Toast.makeText(NewMainActivity.this, "SUCCESS: " + convertedFile.getPath(), Toast.LENGTH_LONG).show();
fileIndex++;
if (this.files.size > fileIndex) {
convertFile(this.files[fileIndex];
} else {
// we're done converting
}
}
#Override
public void onFailure(Exception error) {
Toast.makeText(NewMainActivity.this, "ERROR: " + error.getMessage(), Toast.LENGTH_LONG).show();
// cancel out or keep going, whatever
}
};
Toast.makeText(this, "Converting audio file..." + filename, Toast.LENGTH_SHORT).show();
AndroidAudioConverter.with(this)
.setFile(wavFile)
.setFormat(AudioFormat.MP3)
.setCallback(callback)
.convert();
}
}
}
This question already has an answer here:
Android send mail with PDF file
(1 answer)
Closed 7 years ago.
I need send a PDF file attach on a message, I have a button that calls a function that open a Intent with message, email address and subject filled, but I need that the PDF file has been attached too.
This is my code and I can not find my error, someone can help me please?
public void initializeWebView() {
// Initialize the webview
webView.setResourceClient(new XWalkResourceClient(webView) {
#Override
public boolean shouldOverrideUrlLoading(XWalkView view, String stringUrl) {
if(stringUrl.equals(baseUrl)) {
return false;
}
// mailto links will be handled by the OS.
if (stringUrl.startsWith("mailto:")) {
Uri uri = Uri.parse(stringUrl);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
String fileName = "bouhnik.pdf";
String filePath = (Configuration.getMagazineAssetPath()).toString()+ File.separator + fileName;
Context c = getActivity().getApplicationContext();
File file = null;
FileOutputStream fos = null;
try {
InputStream is = c.getAssets().open(filePath);
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
fos = new FileOutputStream(file);
fos.write(buffer);
fos.close();
} catch (IOException e) {
Log.i("Ferrou",e.toString());
e.printStackTrace();
}
if (!file.exists() || !file.canRead()) {
return false;
}
intent.putExtra(intent.EXTRA_STREAM, file.getPath());
intent.setClassName("com.android.email", "com.android.mail.compose.ComposeActivity");
intent .putExtra(Intent.EXTRA_SUBJECT, "Subject");
WebViewFragment.this.startActivity(Intent.createChooser(intent, "Send email..."));
} else {
try {
URL url = new URL(stringUrl);
// We try to remove the referrer string to avoid passing it to the server in case the URL is an external link.
String referrer = "";
if (url.getQuery() != null) {
Map<String, String> variables = Configuration.splitUrlQueryString(url);
String finalQueryString = "";
for (Map.Entry<String, String> entry : variables.entrySet()) {
if (entry.getKey().equals("referrer")) {
referrer = entry.getValue();
} else {
finalQueryString += entry.getKey() + "=" + entry.getValue() + "&";
}
}
if (!finalQueryString.isEmpty()) {
finalQueryString = "?" + finalQueryString.substring(0, finalQueryString.length() - 1);
}
stringUrl = stringUrl.replace("?" + url.getQuery(), finalQueryString);
}
// Remove referrer from query string
if (!url.getProtocol().equals("file")) {
if (referrer.equals(WebViewFragment.this.getActivity().getString(R.string.url_external_referrer))) {
Uri uri = Uri.parse(stringUrl);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
WebViewFragment.this.startActivity(intent);
} else if (referrer.toLowerCase().equals(WebViewFragment.this.getActivity().getString(R.string.url_baker_referrer))) {
((IssueActivity) WebViewFragment.this.getActivity()).openLinkInModal(stringUrl);
return true;
} else {
return false;
}
} else {
stringUrl = url.getPath().substring(url.getPath().lastIndexOf("/") + 1);
int index = ((IssueActivity) WebViewFragment.this.getActivity()).getJsonBook().getContents().indexOf(stringUrl);
if (index != -1) {
Log.d(this.getClass().toString(), "Index to load: " + index + ", page: " + stringUrl);
((IssueActivity) WebViewFragment.this.getActivity()).getViewPager().setCurrentItem(index);
view.setVisibility(View.GONE);
} else {
// If the file DOES NOT exist, we won't load it.
File htmlFile = new File(url.getPath());
if (htmlFile.exists()) {
return false;
}
}
}
} catch (MalformedURLException | UnsupportedEncodingException ex) {
Log.d(">>>URL_DATA", ex.getMessage());
}
}
return true;
}
});
// Set UI Client (Start stop animations)
webView.setUIClient(new XWalkUIClient(webView) {
#Override
public void onPageLoadStopped(XWalkView view, String url, LoadStatus status) {
if(!url.isEmpty() && status == LoadStatus.FINISHED) {
if(isUserVisible) {
webView.resumeTimers();
}else{
webView.pauseTimers();
}
}
}
});
webView.load(baseUrl, null);
}
Thank's so much for everyone!!
I solve my problem change the type of Intent to:
Intent emailIntent = new Intent(Intent.ACTION_SEND);
Because this is better to email commands, and I define a emailUri where:
emailUri = Uri.fromFile(file.getAbsoluteFile());
because this get a absolute path with a file inside, and when the email client open, it open this file, not a path.
I add a type at my intent but I select the type of my attachment, so I define:
emailIntent.setType("application/pdf");
And finally:
emailIntent.putExtra(Intent.EXTRA_STREAM, uriMail);
startActivity(emailIntent);
It's works now!! Thanks :D
It looks like something is might be going wrong with your file path. Double check it. Then
1 - You need to add the package name of your application with context.getPackageName()
private String path = Environment.getExternalStorageDirectory().getPath() + context.getPackageName() + "books/"+fileName;
2 - Declare the permission inside your AndroidManifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />