aFileChooser error, get file path shows error in fragment - android

public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQUEST_CHOOSER:
if (resultCode == Activity.RESULT_OK) {
final Uri uri = data.getData();
// Get the File path from the Uri
String path = FileUtils.getPath(this, uri);
// Alternatively, use FileUtils.getFile(Context, Uri)
if (path != null && FileUtils.isLocal(path)) {
File file = new File(path);
}
}
break;
}
}
I'm copying this code from github,this link https://github.com/iPaulPro/aFileChooser,I'm putting this code inside a fragment. In this line, it shows error
String path = FileUtils.getPath(this, uri);
Showing this error:
The method getPath(Context, Uri) in the type FileUtils is not applicable for the arguments (PagesFragment, Uri)
Anyone can help me solve this problem?

try like this,
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQUEST_CHOOSER:
if (resultCode == Activity.RESULT_OK) {
final Uri uri = data.getData();
// Get the File path from the Uri
String path = FileUtils.getPath(getActivity(), uri);
// Alternatively, use FileUtils.getFile(Context, Uri)
if (path != null && FileUtils.isLocal(path)) {
File file = new File(path);
}
}
break;
}
}

Related

To get proper file path

I've selected the file with Intent.ACTION_GET_CONTENT
When I print the path , it doesn't show the exact path.
CODE :
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("text/plain");
startActivityForResult(intent, PICKFILE_RESULT_CODE);
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case PICKFILE_RESULT_CODE:
if (resultCode == Activity.RESULT_OK) {
Uri uri = data.getData();
String filePath = uri.getPath();
Toast.makeText(getActivity().getApplicationContext(),
filePath, Toast.LENGTH_SHORT).show();
}
}
}
output: /document/primary:Documents/Test.txt
expected output: sdcard/Documents/Test.txt

Output Uri from this Android crop image library?

I'm trying to implement SoundCloud's image crop to my app. Here's what I'm doing so far:
call Crop.pickImage((Activity) this) to pick an image
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == Crop.REQUEST_PICK && resultCode == RESULT_OK) {
Uri inputUri = data.getData();
new Crop(inputUri).output(outputUri).asSquare().start((Activity) context);
}
else if (requestCode == Crop.REQUEST_CROP && resultCode == RESULT_OK) {
Uri cropped = data.getData();
}
}
At this point I don't know what should be the outputUri. I can just use the same inputUri as the output, but that would overwrite the original image file. I want to create a new file instead. But I can't create new Uri since Android Studio tells me the newly initialized Uri is abstract and can't be used.
I used following methods for my cropping image library for URI creation as well. Hopefully it will helps you:
private void fileSaving(){
String stateEnvironment = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(stateEnvironment)) {
mFileTemp = ChatUtils.getOutputMediaFile(101);//new File(Environment.getExternalStorageDirectory(), TEMP_PHOTO_FILE_NAME);
} else {
mFileTemp = new File(getFilesDir(), sTEMP_PHOTO_FILE_NAME);
}
}
private void takePicture() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
try {
Uri mImageCaptureUri = null;
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
mImageCaptureUri = Uri.fromFile(mFileTemp);
} else {
// mImageCaptureUri =
// InternalStorageContentProvider.CONTENT_URI;
}
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
mImageCaptureUri);
intent.putExtra("return-data", true);
startActivityForResult(intent, mREQUEST_CODE_TAKE_PICTURE);
} catch (ActivityNotFoundException e) {
}
}

Android. How to determine the type of media file in onActivityResult()?

I get media file from Gallery.
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*,video/*,audio/*");
startActivityForResult(Intent.createChooser(intent, "Select Media"), CameraUtils.REQUEST_GALLERY);
How to determine the type of media file in onActivityResult()?
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
switch (requestCode) {
case CameraUtils.REQUEST_GALLERY:
Uri uri = data.getData();
//what type of media file?
break;
}
}
}
Try this:
ContentResolver cr = this.getContentResolver();
String mime = cr.getType(uri);
Just for reference, in Kotlin we can create an extension function for this.
fun Activity.getMimeType(uri:Uri?):String?{
this.let {
val cr: ContentResolver = it.contentResolver
if (uri == null) return null
return cr.getType(uri)?:null
}
}
Feel free to replace nulls with empty String or something. but this works fines!

Android File Path (Xamarin)

I'm currently trying to select a file with an Intent. My Problem is, that the path returned is not in the right format.
My Intent:
private void selectAudioFile(object sender, EventArgs eventArgs)
{
Intent = new Intent();
Intent.SetType("audio/*");
Intent.SetAction(Intent.ActionGetContent);
StartActivityForResult(Intent.CreateChooser(Intent, "Select Audio File"), PickAudioId);
}
And the rusult method:
protected override void OnActivityResult (int requestCode, Result resultCode, Intent data) {
base.OnActivityResult (requestCode, resultCode, data);
if ((resultCode == Result.Ok) && (requestCode == PickAudioId) && (data != null)) {
Android.Net.Uri uri = data.Data;
if (!File.Exists(uri)) {
// error
}
}
}
The Problem:
I need to handle the received path with the File class. The path looks like /document/audio:1234.
If I check the path with File.Exists(uri) it sais that the file does not exist.
How can i get the path to this file in a format i can handle with File like /storage/emulated/0/Music/foo.mp3 or something like that ?
Thanks for help
The uri you get in return may look like a path but it's not, it's more like a shortcut to a database record.
To retrieve a real path is quite a mouthful, you've got to use a ContentResolver, here's how to retrieve it (based on that sample from Xamarin) :
protected override void OnActivityResult (int requestCode, Result resultCode, Intent data) {
base.OnActivityResult (requestCode, resultCode, data);
if ((resultCode == Result.Ok) && (requestCode == PickAudioId) && (data != null)) {
string path = GetPathToImage(data.Data);
if (!File.Exists (path)) {
Log.Debug ("","error");
} else {
Log.Debug ("","ok");
}
}
}
private string GetPathToImage(Android.Net.Uri uri)
{
string path = null;
// The projection contains the columns we want to return in our query.
string[] projection = new[] { Android.Provider.MediaStore.Audio.Media.InterfaceConsts.Data };
using (ICursor cursor = ManagedQuery(uri, projection, null, null, null))
{
if (cursor != null)
{
int columnIndex = cursor.GetColumnIndexOrThrow(Android.Provider.MediaStore.Audio.Media.InterfaceConsts.Data);
cursor.MoveToFirst();
path = cursor.GetString(columnIndex);
}
}
return path;
}
you can also add other Android.Provider.MediaStore.Audio.Media.InterfaceConsts.XXX values to get more info about the file
++

File path from Camera Intent

I'm trying to get the file path of pictures that I took with Camera Intent as a String, but the String filePath is always null. What am I doing wrong?
public void onClick(View arg0) {
switch (arg0.getId()) {
case R.id.btnImageCapture:
Intent openCamera = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(openCamera, OPEN_CAMERA);
break;
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(requestCode){
case OPEN_CAMERA:
if (resultCode == RESULT_OK && data != null) {
Uri captureImage = data.getData();
String filePath = captureImage.getPath();
break;
}
}
}
This is how I get the image that was taken with the camera.
I create the file before and when the image gets saved then it gets saved to my file..
File externalFile = new File("Whatever you want the path to be...");
Uri uriSavedImage=Uri.fromFile(externalFile);
Intent launchcameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
launchcameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
startActivityForResult(launchcameraIntent,CAMERA_PIC_REQUEST);
Then when the result is received.
protected void onActivityResult(int requestCode,int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
if (requestCode == CAMERA_PIC_REQUEST) {
Bitmap photo = BitmapUtils.decodeFileForDisplay(new File("Whatever your file's path is");
}
}
}
try the following passing your captureImage Uri as parameter:
public String getRealPathFromURI(Uri contentUri) {
String[] projx = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(contentUri, projx, null, null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
EDIT:
that is a common bug that getData() returns null on some devices. You need to use a pre-inserted Uri to prevent that. Example:
Intent cameraIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
preinsertedUri = getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
new ContentValues());
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
Getting the result:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case CAMERA_PIC_REQUEST:
if (resultCode != 0 && data != null) {
Uri imageUri = preinsertedUri;
}
break;
}

Categories

Resources