My Code is :
File file = new File(docpath);
String mimetype = "application/pdf";
Uri path = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, mimetype);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
If I want to use the application/pdf method
What should I do?
PDFs are not naturally supported in android. You will need to use an open source library.
Android PDF Viewer, for example.
http://code.google.com/p/apv/
Related
I am working on a demo to share multiple photos on different social platforms. I was able to post a single picture using the code on Instagram but i am not sure how to post multiple images on it. I am not sure even if Instagram provides such functionality.
Try like this
ArrayList<Uri> images = new ArrayList<Uri>();
Files[] imagesFiles; //your imagesFile
for(String path : imagesFiles) {
File file = new File(path);
Uri uri = Uri.fromFile(file);
files.add(uri);
}
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND_MULTIPLE);
intent.putExtra(Intent.EXTRA_SUBJECT, images);
intent.setType("image/jpeg");
You'll need to use Intent.ACTION_SEND_MULTIPLE.
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND_MULTIPLE);
intent.putExtra(Intent.EXTRA_SUBJECT, "Here are some files.");
intent.setType("image/jpeg"); /* This example is sharing jpeg images. */
intent.setPackage("com.instagram.android");
ArrayList<Uri> files = new ArrayList<Uri>();
for(String path : filesToSend /* List of the files you want to send */) {
File file = new File(path);
Uri uri = Uri.fromFile(file);
files.add(uri);
}
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, files);
startActivity(intent);
This could definitely be simplified but I left some lines in so you can break down each step that is needed.
NOTE: Starting in API 24, sharing file URIs will cause a FileUriExposedException. You can either switch your compileSdkVersion to 23 or lower or you can use content URIs with a FileProvider.
I want people to be able to send a direct message to my Instagram account from my android application.
What is the intent for sending a direct message on Instagram?
Thanks
You can use below code
String type = "video/*";
String filename = "/myVideo.mp4";
String mediaPath = Environment.getExternalStorageDirectory() + filename;
createInstagramIntent(type, mediaPath);
private void createInstagramIntent(String type, String mediaPath){
// Create the new Intent using the 'Send' action.
Intent share = new Intent(Intent.ACTION_SEND);
// Set the MIME type
share.setType(type);
// Create the URI from the media
File media = new File(mediaPath);
Uri uri = Uri.fromFile(media);
// Add the URI to the Intent.
share.putExtra(Intent.EXTRA_STREAM, uri);
share.putExtra(Intent.EXTRA_STREAM, [URI of photo]);
share.putExtra(Intent.EXTRA_TEXT, [Text of caption]);
// Broadcast the Intent.
startActivity(Intent.createChooser(share, "Share to"));
}
See official documentation
See this & this also
To Share plain text to the Instagram DM(Direct) use following:
val shareIntent = Intent(Intent.ACTION_SEND)
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, "Assd") // set uri
shareIntent.setPackage("com.instagram.android")
startActivity(shareIntent)
Or You Also can use a file instead:
String type = "video/*";
...
intent..putExtra(Intent.EXTRA_STREAM, [URI of photo]);
I'm getting this exception
06-28 21:00:48.792: W/System.err(10272): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=file:///mnt/sdcard/01 - 3G - Kaise Bataaoon [SongsPK.info] (2).mp3 typ=audio/mpeg }
I'm trying to open this file via these code :
File sdCard = Environment.getExternalStorageDirectory();
Intent intnt = new Intent(Intent.ACTION_VIEW);
String ext = fileToOpen.substring(fileToOpen.lastIndexOf('.')+1) ;
//Intent j = Intent.createChooser(intnt, "Choose an application to open with:");
//startActivity(j);
File fl = new File(sdCard.getAbsolutePath()+"/"+ fileToOpen);
if(fl.exists())
{
Log.d("FILEMANAGERACTIVITY",fl.getPath() + "0"+ ext);
MimeTypeMap mim = MimeTypeMap.getSingleton();
String type= mim.getMimeTypeFromExtension(ext);
intnt.setDataAndType(Uri.fromFile(fl) , type);
startActivity(intnt);
}
Make sure the data and type you are sending in intnt.setDataAndType(Uri.fromFile(fl) , type); is correct, seems like the Intent.ACTION_VIEW do not have any match for that intent data and hence there's no activity to handle that intent...
Regards!
add the activty to mainfist.xml may help you
Here is my code, but this is for a single file solution.
Can I share multiple files & uploads like I do for single files below?
Button btn = (Button)findViewById(R.id.hello);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_SEND);
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/pic.png";
File file = new File(path);
MimeTypeMap type = MimeTypeMap.getSingleton();
intent.setType(type.getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(path)));
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
intent.putExtra(Intent.EXTRA_TEXT, "1111");
startActivity(intent);
}
});
Yes but you'll need to use Intent.ACTION_SEND_MULTIPLE instead of Intent.ACTION_SEND.
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND_MULTIPLE);
intent.putExtra(Intent.EXTRA_SUBJECT, "Here are some files.");
intent.setType("image/jpeg"); /* This example is sharing jpeg images. */
ArrayList<Uri> files = new ArrayList<Uri>();
for(String path : filesToSend /* List of the files you want to send */) {
File file = new File(path);
Uri uri = Uri.fromFile(file);
files.add(uri);
}
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, files);
startActivity(intent);
This could definitely be simplified but I left some lines in so you can break down each step that is needed.
UPDATE: Starting in API 24, sharing file URIs will cause a FileUriExposedException. To remedy this, you can either switch your compileSdkVersion to 23 or lower or you can use content URIs with a FileProvider.
UPDATE (to the update): Google recently announced that new apps and app updates would be required to target one of the latest versions of Android for release to the Play Store. That said, targeting API 23 or lower is no longer a valid option if you plan to release the app to the store. You must go the FileProvider route.
Here is little improved version improvised by MCeley's solution. This could be used to send the heterogeneous file list (like image, document and video at same time), for instance uploading downloaded documents, images at same time.
public static void shareMultiple(List<File> files, Context context){
ArrayList<Uri> uris = new ArrayList<>();
for(File file: files){
uris.add(Uri.fromFile(file));
}
final Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
intent.setType("*/*");
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
context.startActivity(Intent.createChooser(intent, context.getString(R.string.ids_msg_share)));
}
If you are sharing a file with another applications on devices running KitKat and above, you will need to provide Uri permissions.
This is how I handle multiple file sharing pre and post KitKat:
//All my paths will temporarily be retrieve into this ArrayList
//PathModel is a simple getter/setter
ArrayList<PathModel> pathList;
//All Uri's are retrieved into this ArrayList
ArrayList<Uri> uriArrayList = null;
//This is important since we are sending multiple files
Intent sharingIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
//Used temporarily to get Uri references
Uri shareFileUri;
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
//My paths are stored in SQLite, I retrieve them first
SQLiteHelper helper = new SQLiteHelper(this);
pathList = helper.getAllAttachments(viewholderID);
helper.close();
//Create new instance of the ArrayList where the Uri will be stored
uriArrayList = new ArrayList<>();
//Get all paths from my PathModel
for (PathModel data : pathList) {
//Create a new file for each path
File mFile = new File(data.getPath());
//No need to add Uri permissions for pre-KitKat
shareFileUri = Uri.fromFile(mFile);
//Add Uri's to the Array that holds the Uri's
uriArrayList.add(shareFileUri);
}
} else {
//My paths are stored in SQLite, I retrieve them first
SQLiteHelper helper = new SQLiteHelper(this);
pathList = helper.getAllAttachments(viewholderID);
helper.close();
//Create new instance of the ArrayList where the Uri will be stored
uriArrayList = new ArrayList<>();
//Get all paths from my PathModel
for (PathModel data : pathList) {
//Create a new file for each path
File mFile = new File(data.getPath());
//Now we need to grant Uri permissions (kitKat>)
shareFileUri = FileProvider.getUriForFile(getApplication(), getApplication().getPackageName() + ".provider", mFile);
//Add Uri's to the Array that holds the Uri's
uriArrayList.add(shareFileUri);
}
//Grant read Uri permissions to the intent
sharingIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
//I know that the files which will be sent will be one of the following
sharingIntent.setType("application/pdf/*|image|video/*");
//pass the Array that holds the paths to the files
sharingIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uriArrayList);
//Start intent by creating a chooser
startActivity(Intent.createChooser(sharingIntent, "Share using"));
In my case the paths were stored in SQLite, but the paths can come from wherever.
/*
manifest file outside the applicationTag write these permissions
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> */
File pictures = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
//Get a top-level public external storage directory for placing files of a particular type.
// This is where the user will typically place and manage their own files,
// so you should be careful about what you put here to ensure you don't
// erase their files or get in the way of their own organization...
// pulled from Standard directory in which to place pictures that are available to the user to the File object
String[] listOfPictures = pictures.list();
//Returns an array of strings with the file names in the directory represented by this file. The result is null if this file is not a directory.
Uri uri=null;
ArrayList<Uri> arrayList = new ArrayList<>();
if (listOfPictures!=null) {
for (String name : listOfPictures) {
uri = Uri.parse("file://" + pictures.toString() + "/" + name );
arrayList.add(uri);
}
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND_MULTIPLE);
intent.putExtra(Intent.EXTRA_STREAM, arrayList);
//A content: URI holding a stream of data associated with the Intent, used with ACTION_SEND to supply the data being sent.
intent.setType("image/*"); //any kind of images can support.
chooser = Intent.createChooser(intent, "Send Multiple Images");//choosers title
startActivity(chooser);
}
I'm trying to share an image file which saved to internal storage using share intent.
after I intent image file doesn't display.
I used following code to save the image file
try {
FileOutputStream fos = openFileOutput("twitterimage.jpg", Context.MODE_PRIVATE);
// Writing the bitmap to the output stream
bikeBm.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.close();
} catch (Exception e) {
Log.e("saveToInternalStorage()", e.getMessage());
}
//intent code
File internalFile = getFileStreamPath("twitterimage.jpg");
Uri uri = Uri.fromFile(internalFile);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM,new File(uri));
shareIntent.setType("image/jpeg");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(shareIntent, "send"));
when I check the file existance using above "uri", the image file exists
in the internal storage but checking the file using
String file = "data/data/com.bike.app/twitterimage.jpg";
File f = new File(file);
shows me the file not exist.
pls help me to solve this problem.
If i am gessing right you want to share images between to applications. Your probleme is that you used Context.MODE_PRIVATE when you write a file so other applications cannot have access to this file.
To share an image you can use MediaProvider to restrict access to you Media or simply used Context.MODE_WORLD_READABLE but the file will be public readable then.
hope that help