I'm developing a video converter in android studio. I'm able to convert file successfully and can play the resulting file with:
File file = new File(filepath);
Uri path = Uri.fromFile(file);
Intent Openintent = new Intent(Intent.ACTION_VIEW);
Openintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Openintent.setDataAndType(path, "audio/mp3");
try {
startActivity(Openintent);
}
catch (ActivityNotFoundException e) {
}
But I can't find any code to open the resulting folder and show converted file marked. Like we have in windows Locate file in folder.
here is how you can open the containing folder for the converted file
String folderPath = filepath.substring(0 , filepath.lastIndexOf('/') + 1);
Uri uri = Uri.parse(folderPath);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "resource/folder");
startActivity(intent);
Related
How to detect from which (any) external app(or packagname) data was shared:
Via sharing an image/data (intent) from another app to my app(activity),
private void shareImage() {
Intent share = new Intent(Intent.ACTION_SEND);
// If you want to share a png image only, you can do:
// setType("image/png"); OR for jpeg: setType("image/jpeg");
share.setType("image/*");
// Make sure you put example png image named myImage.png in your
// directory
String imagePath = Environment.getExternalStorageDirectory()
+ "/myImage.png";
File imageFileToShare = new File(imagePath);
Uri uri = Uri.fromFile(imageFileToShare);
share.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(share, "Share Image!"));
}
I've searched for a while on the internet but I didnt find a solution!
In my project I put a PDF file inside the drawable folder (I dont know where else to put it, honestly). That PDF is a menĂ¹ that shows the user all the food he can find in that restaurant.
There is a button that enables the user to open that PDF file. By clicking over it I receive a error message. More or less it says that the App cant file my file: "Impossibile to open menuristorante.pdf".
I created a method to open that file and this is the code I wrote:
public void openMenuRistorante(View view)
{
File pdfFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/menuristorante.pdf");
Uri path = Uri.fromFile(pdfFile);
Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
pdfIntent.setDataAndType(path, "application/pdf");
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try{
startActivity(pdfIntent);
}catch(ActivityNotFoundException e){
Toast.makeText(this, "Nessuna Applicazione per leggere i pdf", Toast.LENGTH_SHORT).show();
}
}
Probably I am wrong putting my file in the wrong directory.. But where have I to put it? Keep in mind that my PDF file must be already into the app, so it must be inside the phone when the user installs this App.
Thanks
Put the PDF file in the assets folder and try using the following code:
Uri file= Uri.parse("file:///android_asset/mypdf.pdf");
String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(file.toString()));
try {
Intent i;
i = new Intent(Intent.ACTION_VIEW);
i.setDataAndType(file,mimeType);
startActivity(i);
} catch (ActivityNotFoundException e) {
Toast.makeText(this,
"No Application Available to view this file type",
Toast.LENGTH_SHORT).show();
}
I'm doing a similar thing in my app, I have my files though in the download folder of my mobile in a sub-folder called "Documents", I guess you could do the same, it's better than keeping it in drawable. here's the code I use :
try {
File file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS + "/Documents/" + fileName);
Intent intent = new Intent("com.adobe.reader");
intent.setType("application/pdf");
intent.setAction(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, "application/pdf");
startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(Documents.this, "Erreur d'ouverture du fichier", Toast.LENGTH_LONG).show();
}
I want to download the file from server and view the file in android with various file Extensions like (pdf,doc,mp4,jpeg)
i have try this
Intent intent = new Intent(Intent.ACTION_VIEW);
File file1 = new File(Environment.getExternalStorageDirectory(),"report.pdf");
intent.setDataAndType( Uri.fromFile( file1 ), "application/.*" );
startActivity(intent);
but no success
should i have to give the individual extension in this
please suggest the way to solve this
See this code for your reference
filePath is a actual path of the file
Uri fileUri = Uri.fromFile(new File(filePath));
Uri new_uri = Uri.parse("file://"
+ fileUri.getPath());
Intent intent = new Intent(Intent.ACTION_VIEW,
new_uri);
MimeTypeMap myMime = MimeTypeMap.getSingleton();
String fileExt = filePath.substring(filePath
.lastIndexOf(".") + 1);
String mimeType = myMime
.getMimeTypeFromExtension(fileExt);
intent.setDataAndType(new_uri, mimeType);
startActivity(intent);
So I am trying to have a directory added to the stock android gallery app, and I can't get it to work. I tried the Stock android approach for that, I have also tried the way that is supplied via AOSP and neither have worked. How might I create a new directory, take a new picture and add it to the gallery. I have the photo saving and everything wokring it's just getting it to display in the Gallery app that isn't working
The way google says to add photos to the gallery is the following way....
private void galleryAddPic() {
Intent mediaScanIntent = new Intent("android.intent.action.MEDIA_SCANNER_SCAN_FILE");
File f = new File(mCurrentPhotoPath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
}
I personally have it set up for
private void galleryAddPic() {
Intent mediaScanIntent = new Intent("android.intent.action.MEDIA_SCANNER_SCAN_FILE");
File f = new File(mCurrentPhotoPath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
Config.context.sendBroadcast(mediaScanIntent);
}
where Config.context is initialized in MainActivity to getApplicationContext();
So I changed my function to the following
private void galleryAddPic(String Location) {
Intent mediaScanIntent = new Intent("android.intent.action.MEDIA_SCANNER_SCAN_FILE");
File f = new File(Location);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
getActivity().sendBroadcast(mediaScanIntent);
}
And to call the function
galleryAddPic(image.getAbsolutePath());
where image is a File
Not the way I had it
galleryAddPic("file:"+image.getAbsolutePath());
Hi I have the following code to share an image:
// Share
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
Uri uri = Uri.parse(getFilesDir() + File.separator + "myGoal.jpg");
share.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(share, "Share Image"));
It works to share the image to Dropbox but if I pick the Facebook option, I get Facebook's status update dialog with no image attached and if I try to update my status with 'Test' it doesn't work. No errors. Just not working.
I know it's not the image because it uploads to my Dropbox properly and I can pull up the image and look at it.
Do I have to attach the image to the intent differently for it to work with Facebook?
Any ideas? I'm debugging on a physical device.
So I figured out the problem.
I was saving the picture to internal storage with getFilesDir() which put the picture into my apps sandbox and made inaccessible to the other apps.
I replaced my code with the following:
String file_path = Environment.getExternalStorageDirectory().getAbsolutePath() +
"/MyApp/";
File dir = new File(file_path);
dir.mkdirs();
File file = new File(dir, "myPic.png");
FileOutputStream fOut = new FileOutputStream(file);
screenshot.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
// Share
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/png");
share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
share.putExtra(Intent.EXTRA_TEXT, "My Image");
startActivity(Intent.createChooser(share, "Share Image"));
Works perfectly fine now.
What i have done to post image on facebook is instead of passing it directly i create a function and write it like this :
private void ShareWall(String message) {
Bundle parameters = new Bundle();
// share msg
parameters.putString("message", message);
// shre image which you have define above
parameters.putString("picture", postImage);
try {
facebook.request("me");
String response = facebook.request("me/feed", parameters, "POST");
Log.d("response: ", response);
if (response == null || response.equals("")) {
response.equals("");
showToast("No Response.");
} else {
showToast("Message has been posted to your walll!.");
}
finish();
} catch (Exception e) {
showToast("Message failed to posdt on wall.");
e.printStackTrace();
finish();
}
}
Hope this help you.
You can still share images (but not text) from your app to Facebook even if you are not using the Facebook SDK.
Just make sure that you use Uri.fromFile instead of Uri.parse and it will work:
DO NOT USE:
intent.putExtra(Intent.EXTRA_STREAM, Uri.parse(pathToFile));
USE:
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(pathToFile)));
Here is a solution without using external file write:
Drawable mDrawable = myImageView1.getDrawable();
Bitmap mBitmap = ((BitmapDrawable)mDrawable).getBitmap();
String path = MediaStore.Images.Media.insertImage(getContentResolver(), mBitmap, "Image I want to share", null);
Uri uri = Uri.parse(path);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, "Share Image"));
In this case, my image comes from an ImageView myImageView.