To get proper file path - android

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

Related

aFileChooser error, get file path shows error in fragment

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;
}
}

Pick a file from an SD card using Intent

Does Android have a way to browse and pick any file from an SD card using intents?
Something like:
String uri = (Environment.getExternalStorageDirectory()).getAbsolutePath();
Intent i = new Intent(Intent.ACTION_PICK, Uri.parse(uri));
I am trying to send a file to other devices using Bluetooth. I can send if I give the full path of the file name in my code. I want my users to pick the file that should be send.
You can use the following code:
Intent mediaIntent = new Intent(Intent.ACTION_GET_CONTENT);
mediaIntent.setType("video/*"); // Set MIME type as per requirement
startActivityForResult(mediaIntent,REQUESTCODE_PICK_VIDEO);
Then you can get the path in onActivityResult:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUESTCODE_PICK_VIDEO
&& resultCode == Activity.RESULT_OK) {
Uri videoUri = data.getData();
Log.d("", "Video URI= " + videoUri);
}
}
For general browsing use this (e.g. Music file):
Intent intent = new Intent();
intent.setType("*/*");
if (Build.VERSION.SDK_INT < 19) {
intent.setAction(Intent.ACTION_GET_CONTENT);
intent = Intent.createChooser(intent, "Select file");
} else {
intent.setAction(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
String[] mimetypes = { "audio/*", "video/*" };
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes);
}
startActivityForResult(intent, Constants.REQUEST_BROWSE);
And receive the browsed data here:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == Constants.REQUEST_BROWSE
&& resultCode == Activity.RESULT_OK && data != null) {
Uri uri = data.getData();
if (uri != null) {
// TODO: handle your case
}
}
}

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;
}

File path to String

I'm trying to get the complete path of an image file as a String, but it doesn't work. I'm just getting a result like "content:/external/media/images/1". That's definitely not the correct path. How can i get the correct path including the file extensions?
Here is what i tried so far:
public void onClick(View arg0) {
switch (arg0.getId()) {
case R.id.btnGetImage:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent, "Select A Picture"),
PHOTO_GALLERY);
break;
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(requestCode){
case PHOTO_GALLERY:
if (resultCode == RESULT_OK) {
File file = new File(data.getDataString());
String imagePath = file.getAbsolutePath();
break;
}
}
}
try this code:
case R.id.btnGetImage:
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
break;
and
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK
&& null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
picturePath = cursor.getString(columnIndex);
cursor.close();
ImageView imageView = (ImageView) findViewById(R.id.imageView1);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
picturePath is your required path ...
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(requestCode){
case PHOTO_GALLERY:
if (resultCode == RESULT_OK) {
Uri selectedImageUri = data.getData();
String selectedImagePath = getRealPathFromURI(selectedImageUri);
File file = new File(selectedImagePath );
break;
}
}
}
You have define correct filename to your path which must be exist in your sdcard, while fetch from sdcard,
String filepath = Environment.getExternalStorageDirectory()+"/foldername/"+test.png;
I smell you trying to do something like this.

how to get image name using camera intent in android?

I want to show camera's current take image using like given this code.I can get image from camera and display in imageview.I want to know that image's file name.
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE );
startActivityForResult(intent, CAMERA_PIC_REQUEST);
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode)
{
case 2:
{
if (resultCode == RESULT_OK)
{
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
mg_view.setImageBitmap(thumbnail);
}
break;
}
}
}
How can i get image name?please help friends,
Thanks Friends
In your activity (called YourActivity):
public static int TAKE_IMAGE = 111;
Uri mCapturedImageURI;
Somewhere call the camera!
try {
String fileName = "temp.jpg";
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, fileName);
mCapturedImageURI = getContentResolver()
.insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
values);
Intent intent = new Intent(
MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT,
mCapturedImageURI);
startActivityForResult(intent, TAKE_IMAGE);
} catch (Exception e) {
Log.e("", "", e);
}
Now in the Activity result (notice capturedImageFilePath)
protected void onActivityResult(int requestCode, int resultCode,
Intent intent) {
if ((requestCode == YourActivity.TAKE_IMAGE)
&& (resultCode == RESULT_OK)) {
mode = MODE_VIEWER;
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(mCapturedImageURI, projection, null,
null, null);
int column_index_data = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
//THIS IS WHAT YOU WANT!
String capturedImageFilePath = cursor.getString(column_index_data);
bitmap = BitmapFactory.decodeFile(capturedImageFilePath);
}
}
sample code. May be it is useful to you.
Uri mUri;
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
mUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "imgnm_"+ String.valueOf(System.currentTimeMillis())+ ".jpg"));
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,mUri);
intent.putExtra("return-data", true);
startActivityForResult(intent, PICK_CAMERA_IMAGE);
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_CAMERA_IMAGE) {
if (resultCode == RESULT_OK) {
String path = mUri.getPath();
if (path.length() > 0) {
String filepath = path;
String filename = filepath.substring(filepath.lastIndexOf("/") + 1,filepath.length());
String filetype = ".jpg";
Bitmap bm = BitmapFactory.decodeFile(filepath);
mg_view.setImageBitmap(bm);
}
}
}
Try this code,
Log.d("ANDRO_CAMERA", "Starting camera on the phone...");
String fileName = "testphoto.jpg";
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, fileName);
values.put(MediaStore.Images.Media.DESCRIPTION,
"Image capture by camera");
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
imageUri = getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
startActivityForResult(intent, IMAGE_CAPTURE);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == IMAGE_CAPTURE) {
if (resultCode == RESULT_OK){
Log.d("ANDRO_CAMERA","Picture taken!!!");
imageView.setImageURI(imageUri);
}
}
}
}

Categories

Resources