I want to choose a file via an Android File Manager.
so, to open file intent code is
Intent intent = new Intent();
intent.setType("file/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select File"), 1);
But I was unable to access file manager in samsung devices to resolve this problem i do
Intent intent = new Intent("com.sec.android.app.myfiles.PICK_DATA");
intent.putExtra("CONTENT_TYPE", "*/*");
intent.addCategory(Intent.CATEGORY_DEFAULT);
startActivityForResult(intent, 1);
But this code in not working in other devices so what should i do to open file manager in all devices ?
private void openFile(int requestCODE) {
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.setType("*/*");
startActivityForResult(intent, requestCODE);
}
I have a XML file on the external storage, and I have different apps which are able to open this file format.
I am going to sent implicit intent like this in order to find apps to open XML file:
public static boolean isIntentAvailable(Context context, Intent intent) {
List<ResolveInfo> list = context.getPackageManager().queryIntentActivities(intent,
PackageManager.MATCH_DEFAULT_ONLY);
return !list.isEmpty();
}
This method has 2 arguments. The question is how to create intent for second argument?
I have only path to file which I need to open and I tried this way:
Intent intent = new Intent(**path to file**);
But it doesn't work... How can I create this intent properly?
You could do something like this:
File file = new File("path to file");
//checking if the File exists
if (file.exists()) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "text/xml");
//checking if an Activity exists that can handle this Intent
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
i want open a file with hp ePrint exclusively...but with my code, file is opened with adobe reader...i don't know why....
THANKS IN ADVANCE!
public void viewPDF() {
String path = "/sdcard/droidText/ciccia.pdf";
try {
File targetFile = new File(path);
Uri targetUri = Uri.fromFile(targetFile);
Intent intent=new Intent();
intent.setPackage("com.hp.android.print");
intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(targetUri, "application/pdf");
startActivity(intent);
}catch(ActivityNotFoundException anfe){
final String appPackageName="com.hp.android.print";
try{
startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse("market://details?id=" +appPackageName)));
}catch(android.content.ActivityNotFoundException anfer){
startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse("https://play.google.com/store/apps/details?id=" +appPackageName)));
}
}
}
Intent intent=new Intent();
intent.setPackage("com.hp.android.print")
intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(targetUri, "application/pdf");
You redeclared the intent object.
We are building a camera app that saves photos in a specific folder in the gallery. And we have to open the folder of our app in the gallery using an intent. We are using this code however it shows all folders.
View.OnClickListener goToGallery = new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setType("image/*");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
};
To open gallery I use this intent.
public static final int RESULT_GALLERY = 0;
Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent , RESULT_GALLERY );
Try this code.
It will retrieve view pictures under storage/emulated/0/Pictures/MyAppPics
You can change the file path according to your directory path.
File sdDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File f = new File(sdDir, "MyAppPics");
Intent i = new Intent();
i.setAction(Intent.ACTION_VIEW);
i.setDataAndType(Uri.withAppendedPath(Uri.fromFile(f), "/MyAppPics"), "image/*");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
I'm trying to pass a URI-Object to my Intent in order to use that URI
in another activity.
How do I pass a URI?
private Uri imageUri;
....
Intent intent = new Intent(this, GoogleActivity.class);
intent.putExtra("imageUri", imageUri);
startActivity(intent);
this.finish();
How do I use now this URI in my other activity?
imageUri = extras.getString("imageUri"); // I know thats wrong ...
you can store the uri as string
intent.putExtra("imageUri", imageUri.toString());
and then just convert the string back to uri like this
Uri myUri = Uri.parse(extras.getString("imageUri"));
The Uri class implements Parcelable, so you can add and extract it directly from the Intent
// Add a Uri instance to an Intent
intent.putExtra("imageUri", uri);
// Get a Uri from an Intent
Uri uri = intent.getParcelableExtra("imageUri");
You can use the same method for any objects that implement Parcelable, and you can implement Parcelable on your own objects if required.
In Intent, you can directly put Uri. You don't need to convert the Uri to string and convert back again to Uri.
Look at this simple approach.
// put uri to intent
intent.setData(imageUri);
And to get Uri back from intent:
// Get Uri from Intent
Uri imageUri=getIntent().getData();
private Uri imageUri;
....
Intent intent = new Intent(this, GoogleActivity.class);
intent.putExtra("imageUri", imageUri.toString());
startActivity(intent);
this.finish();
And then you can fetch it like this:
imageUri = Uri.parse(extras.getString("imageUri"));
here how I use it;
This button inside my CameraActionActivity Activity class where I call camera
btn_frag_camera.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intenImatToSec = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
startActivityForResult(intenImatToSec, REQUEST_CODE_VIDEO);
//intenImatToSec.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
//intenImatToSec.putExtra(MediaStore.EXTRA_DURATION_LIMIT, 10);
//Toast.makeText(getActivity(), "Hello From Camera", Toast.LENGTH_SHORT).show();
}
});
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == REQUEST_CODE_IMG) {
Bundle bundle = data.getExtras();
Bitmap bitmap = (Bitmap) bundle.get("data");
Intent intentBitMap = new Intent(getActivity(), DisplayImage.class);
// aldıgımız imagi burda yonlendirdiğimiz sınıfa iletiyoruz
ByteArrayOutputStream _bs = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 50, _bs);
intentBitMap.putExtra("byteArray", _bs.toByteArray());
startActivity(intentBitMap);
} else if (requestCode == REQUEST_CODE_VIDEO) {
Uri videoUrl = data.getData();
Intent intenToDisplayVideo = new Intent(getActivity(), DisplayVideo.class);
intenToDisplayVideo.putExtra("videoUri", videoUrl.toString());
startActivity(intenToDisplayVideo);
}
}
}
And my other DisplayVideo Activity Class
VideoView videoView = (VideoView) findViewById(R.id.videoview_display_video_actvity);
Bundle extras = getIntent().getExtras();
Uri myUri= Uri.parse(extras.getString("videoUri"));
videoView.setVideoURI(myUri);
If you want to use standard extra data field, you would do something like this:
private Uri imageUri;
....
Intent intent = new Intent(this, GoogleActivity.class);
intent.putExtra(Intent.EXTRA_STREAM, imageUri.toString());
startActivity(intent);
this.finish();
The documentation for Intent says:
EXTRA_STREAM added in API level 1
String EXTRA_STREAM
A content: URI holding a stream of data associated with the Intent,
used with ACTION_SEND to supply the data being sent.
Constant Value: "android.intent.extra.STREAM"
You don't have to use the built-in standard names, but it's probably good practice and more reusable. Take a look at the developer documentation for a list of all the built-in standard extra data fields.
The Uri.parse(extras.getString("imageUri")) was causing an error:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Intent android.content.Intent.putExtra(java.lang.String, android.os.Parcelable)' on a null object reference
So I changed to the following:
intent.putExtra("imageUri", imageUri)
and
Uri uri = (Uri) getIntent().get("imageUri");
This solved the problem.
you can do like this. imageuri can be converted into string like this.
intent.putExtra("imageUri", imageUri.toString());