I'm using file helper class from this post https://stackoverflow.com/a/20559175/2281821 but it doesn't working properly in each case. I'm using Intent chooseIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT); to start file picker. I have Android Nougat and I'm receiving from onActivityResult uri: content://com.android.externalstorage.documents/document/home%3Aimage.png
What does this home: means? How can I get access to this file? I was trying with Environment.getExternalStorageDirectory() and System.getenv("EXTERNAL_STORAGE") but I can't get acess.
Following code can be used to get document path:
Intent galleryIntent = new Intent();
galleryIntent .setType("image/*");
galleryIntent .setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(galleryIntent , getString(R.string.app_name)),REQUEST_CODE);
OnActivityResult():
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == REQUEST_CODE) {
Uri uri = data.getData();
}
}
}
You can use "uri" variable value to get bitmap. If you want to open document other image also then remove following line:
galleryIntent .setType("image/*");
You get access to a content scheme by opening for instance an InputStream on the uri.
InputStream is = getContentResolver().openInputStream(uri);
BitmapFactory.decodeFromStream(is) will happily read from your stream.
Related
I'm a junior in android development and I faced a problem with getting Uri for a long time. My aim is get an Image Uri and show the image after few days. I use this method and it works when I restart my app, but in doesn't work when I try to upload image using the same Uri after a day. What i should do to get long Uri?
Intent galleryIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, 1);
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 1 && resultCode == RESULT_OK){
Uri uri = data.getData();
save(uri.toString());
}
}
because of this URL refers to a local image and I think will not work if you removed this image or renamed it,
you can transform it to base64 and save it in shared preferences or local DB.
I am a beginner at Android programming and I had a doubt to be clarified.
I tried out a tutorial on VideoView in Android and observed that,
When the specified URI string is "http://www.androidbegin.com/tutorial/AndroidCommercial.3gp", the program works.
I tried replacing the URI string with the location of a video present in the phone's internal storage (/storage/emulated/0/Movies/test.mp4) and the program produced the error java.io.IOException: setDataSource failed.
My question is what does the error signify and why does it occur ? since both the URI string's do specify the video to be played.
(Note: I followed this tutorial)
Try this code to get a video from gallery:
// in onCreate method
Intent getVid= new Intent();
getVid.setType("video/*");
getVid.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(getVid, "Select a video" ),
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
} if (requestCode == 1 && resultCode == RESULT_OK){
String videoUrl = data.getData().toString();
Intent i = new Intent(MainActivity.this , videoViewActivity.class);
i.putExtra("vid" , videoUrl);
startActivity(i);
}
in videoViewActivity type this code in onCreate method after initializing videoView :
String path = getIntent().getStringExtra("vid");
videoView.setVideoPath(path);
videoView.start();
It's probably because of your Uri. When you use Uri.parse("/blabla") it's not validating that path, is it really exist or not. And in your case you need to give something like "file:///storage/emulated/0/Movies/test.mp4". Or your app don't have file permission, you need to add a permission check first.
I am developing a file uploading android application. My objective is to upload the user selected file from file manager to a remote server. But when a google drive file is selected , file uploading fails because of empty path . Can somebody help me ?
My code is :
private void showFileChooser() {
Intent intent = new Intent();
intent.setType("*/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
startActivityForResult(Intent.createChooser(intent, "Choose File to Upload.."), PICK_FILE_REQUEST);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == PICK_FILE_REQUEST) {
if (data == null) {
//no data present
return;
}
Uri selectedFileUri = data.getData();
selectedFilePath = FilePath.getPath(mActivity, selectedFileUri);
if (selectedFilePath != null && !selectedFilePath.equals("")) {
callUploadDocumentAPI();
} else {
Toast.makeText(mActivity, StringConstants.CANT_UPLOAD_TO_SERVER, Toast.LENGTH_SHORT).show();
}
}
}
But when a google drive file is selected , file uploading fails because of empty path .
FilePath.getPath(mActivity, selectedFileUri) cannot work. A Uri is not a file.
Use a ContentResolver and openInputStream() to get an InputStream on the content identified by the Uri. Either use that InputStream directly, or use it to make your own copy of the content in some file that you control, then use that copy.
I'm making an app that picks a image from the gallery and returns the result. I need to get the file path from that selection and store it for later use. I have looked at similar questions and apparently the file path of the image is stored in the column
MediaStore.Images.Media.DATA
How would anyone know that this is the file path? I've looked at the android documentation on MediaStore and it does not tell you where the file path column is. Any help would be appreciated.
Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, SELECT_PHOTO);
use this to get an image from gallery, it will return URI of the image which could be store store for later use
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != RESULT_CANCELED) {
if (requestCode == SELECT_PHOTO) {
Uri path = data.getData();
}
}
}
I am making an android album app where I can create an album and add photos and delete photos from the album. Adding a photo is a bit tricky where I need a photo filename with a file path. This was very easy using JFileChooser in java but this is android and I have no clue on getting the filename and file path. Is there any thing in the android api where I can get the same functionality as the JFileChooser.
I am looking for a solution to this problem either using a file chooser of some sort or an entire to new approach. Any help is appreciated..
Or is there any other approach I can implement to add a photo...
You may use Intent.ACTION_PICK to invoke an image picker. This intent may be caught by the default gallery app, or some other app installed on the device.
private static final int REQUEST_PICKER = 1;
private void invokePicker() {
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent, "Complete action using"), REQUEST_PICKER);
}
Then receive the result on onActivityResult.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_OK)
return;
if (requestCode == PICK_FROM_FILE) {
// Get Uri of the file selected,
Uri theImageUri = data.getData();
// Or if you want a Bitmap,
Bitmap theBitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), theImageUri);
}
}
Edited:
Though in this way you don't need a real file path, you can get it from MediaStore if you need.