I am using the following Intent to allow the user to choose a picture
Intent pictureIntent = new Intent();
pictureIntent.setType("image/*");
pictureIntent.setAction(Intent.ACTION_PICK);
startActivityForResult(pictureIntent, GALLERY_PICK_IMAGE_REQUEST);
After getting the result, I am using the following approach (using this method)
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == GALLERY_PICK_IMAGE_REQUEST) {
if (resultCode == mActivity.RESULT_OK) {
Uri selectedImage = data.getData();
Log.d(TAG, "Gallery image path = " + selectedImage.getPath());
launchUploadImageActivity(getRealPathFromURI(mActivity, selectedImage));
}
}
}
private String getRealPathFromURI(Context context, Uri contentUri) {
Cursor cursor = null;
try {
String[] proj = { MediaStore.Images.Media.DATA };
cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} finally {
if (cursor != null) {
cursor.close();
}
}
}
When I use the new Photos application to choose the picture, I can choose a photo that I presume is on google's servers, since it returns a url to a picture, instead of the file path:
Log output:
Gallery image path = /0/https://lh6.googleusercontent.com/lRkls4SQwi_afJvjO5QChsWqRwTpDjg-....
Is there any way I can force the user to choose pictures that are local on the phone?
Try with following code to chose picture
Intent pictureIntent = new Intent(MediaStore.ACTION_PICK, Images.Media.INTERNAL_CONTENT_URL);
startActivityForResult(pictureIntent, REQUEST_CHOOSE_IMAGE);
instead of
Intent pictureIntent = new Intent();
pictureIntent.setType("image/*");
pictureIntent.setAction(Intent.ACTION_PICK);
startActivityForResult(pictureIntent, GALLERY_PICK_IMAGE_REQUEST);
Add this line to your intent:
pictureIntent.setData(Images.Media.INTERNAL_CONTENT_URI);
Related
I am trying to get the real path of a URI.
First I start the gallery app through an intent:
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select picture"), SELECT_IMAGE );
After that the onActivityResult gets called, where I try to get the absolut path of the URI.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
if (resultCode == RESULT_OK) {
Uri uri;
if (requestCode == SELECT_IMAGE) {
final Uri uri_data = data.getData();
// Get the path from the Uri
final String path = getPathFromURI(uri_data);
if (path != null) {
File f = new File(path);
uri = Uri.fromFile(f);
}
}
}
} catch (Exception e) {
Log.e("FileSelectorActivity", "File select error", e);
}
}
The uri_data contains "content://com.android.providers.media.documents/document/image%3A67", but the resulting path is null.
The pathFromUri method looks like this:
private String getPathFromURI(Uri contentUri) {
String res = null;
String[] proj = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(contentUri, proj, null, null, null);
if (cursor.moveToFirst()) {
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
res = cursor.getString(column_index);
}
cursor.close();
return res;
}
Here the column_index is 0, but cursor.getString returns null.
Why does this happen?
I have the permission android.permission.READ_EXTERNAL_STORAGE
EDIT:
So I want to take a picture through the camera app, and the user then chooses the image. So the file should be acutally stored on the phone (which is, since I am testing it on my phone)
EDIT:
Okey, the solution was to request the permissions at runtime...sorry guys
we want to choose a pdf file using intent from a gallery so how can get a real path of a pdf file. We want to choose a PDF file from a gallery and upload this file on a server.
public String getPathFromURI(Context context, Uri contentUri) {
if ( contentUri.toString().indexOf("file:///") > -1 ){
return contentUri.getPath();
}
Cursor cursor = null;
try {
String[] proj = { MediaStore.Images.Media.DATA };
cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}finally {
if (cursor != null) {
cursor.close();
}
}
}
Hi here is sample code for selecting pdf file from external storage and get file's path using which you can access file's data and upload data on your server.
Here PICK_IMAGE is any number you want as your request code.
Intent intent = new Intent();
intent.setType("application/pdf");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select File"),PICK_IMAGE);
public void onActivityResult(int requestCode, int resultCode, final Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE) {
try {
Uri uri1 = data.getData();
String path = String.valueOf(uri1);
String path_lastPart = path.substring(path.indexOf("/storage"));
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
// Do the file write
path_lastPart = path_lastPart.replace("%20", " ");
File yourFile = new File(path_lastPart);
} else {
// Request permission from the user
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
}
} catch (Exception e2) {
Log.e("macro", "" + e2);
}
}
}
I'm developing an app, and in that app I have one button, named 'choose sound'. When user will click this button, he/she should be asked to choose any audio file from the file manager/memory.
So, I know that for this, I'll have to use Intent.Action_GetData. I'm doing the same:
//code start
Intent intent = new Intent();
intent.setType("audio/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent,1);
#Override
protected void onActivityResult(int requestCode,int resultCode,Intent data){
if(requestCode == 1){
if(resultCode == RESULT_OK){
//the selected audio.
Uri uri = data.getData();
int SoundID=soundPool.Load(uri.toString(), 1);
//SoundPool is already constructed and is working perfectly for the resource files
PlaySound(SoundID);
//PlaySound method is already defined
}
}
super.onActivityResult(requestCode, resultCode, data);
}
//end of code
but it's not working
Now, in OnActivityResult, I'm not getting that how to load the proper URI of the file selected by user, because before Android 4.4, it returns the different URI and after Android 4.4 it returns the different URI on intent.GetData();. Now, what I have to do?
Also, I know that for playing the audio file, I'll have to use SoundPool, and I have the code for that too, in fact it's working fine for the resource/raw/audio files, but how to load/play files in SoundPool from this URI?
In your onActivityResult(), do the following changes:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK && data != null)
{
String realPath = null;
Uri uriFromPath = null;
realPath = getPathForAudio(YourActivity.this, data.getData());
uriFromPath = Uri.fromFile(new File(realPath)); // use this uriFromPath for further operations
}
}
Add this method in your Activity:
public static String getPathForAudio(Context context, Uri uri)
{
String result = null;
Cursor cursor = null;
try {
String[] proj = { MediaStore.Audio.Media.DATA };
cursor = context.getContentResolver().query(uri, proj, null, null, null);
if (cursor == null) {
result = uri.getPath();
} else {
cursor.moveToFirst();
int column_index = cursor.getColumnIndex(MediaStore.Audio.AudioColumns.DATA);
result = cursor.getString(column_index);
cursor.close();
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally {
if (cursor != null) {
cursor.close();
}
}
return result;
}
Hope It will do your job. You can play audio using MediaPlayer class also
You can put below codes in your project when you want to select audio.
Intent intent_upload = new Intent();
intent_upload.setType("audio/*");
intent_upload.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent_upload,1);
And override onActivityResult in the same Activity, as below
#Override
protected void onActivityResult(int requestCode,int resultCode,Intent data){
if(requestCode == 1){
if(resultCode == RESULT_OK){
//the selected audio.
Uri uri = data.getData();
}
}
super.onActivityResult(requestCode, resultCode, data);
}
Try to get the path :
//method to get the file path from uri
public String getPath(Uri uri) {
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
cursor.moveToFirst();
String document_id = cursor.getString(0);
document_id = document_id.substring(document_id.lastIndexOf(":") + 1);
cursor.close();
cursor = getContentResolver().query(
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
null, MediaStore.Images.Media._ID + " = ? ", new String[]{document_id}, null);
cursor.moveToFirst();
String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
cursor.close();
return path;
}
then load it :
s2 = soundPool.load(YOU_PATH, PRIORITY);
I'm new to android and to java trying to learn my way in.
Right now I am trying to achieve (A)Select image form gallery (B) show a preview and ( C ) activity is Uploading to server:
Select image form gallery and show a preview: done (achieved) by using below Code
Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
// call android default gallery
startActivityForResult(intent, PICK_FROM_GALLERY);
}
});
protected void onActivityResult(int requestCode, int resultCode, Intent data) {if (requestCode == PICK_FROM_GALLERY) {
if (resultCode == RESULT_OK) {
fileUri = data.getData();
filePath = getRealPathFromURI(getApplicationContext(), fileUri);
Intent imagePreview = new Intent(MainActivity.this, ImagePreview.class);
imagePreview.putExtra("filePath", filePath);
startActivity(imagePreview);
}
public String getRealPathFromURI(Context context, Uri contentUri) {
Cursor cursor = null;
try {
String[] proj = { MediaStore.Images.Media.DATA };
cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} finally {
if (cursor != null) {
cursor.close();
}}
}
And In ImagePreview.java
Intent imagePreview = getIntent();
// image or video path that is captured in previous activity
filePath = imagePreview.getStringExtra("filePath");
displayImage(filePath);
private void displayImage(String filePath) {
ImageView imageView = (ImageView) findViewById(R.id.imageView1);
imageView.setImageBitmap(BitmapHelper.decodeSampledBitmap(filePath, 300, 250));
Now According to my understanding, since the filePath is already a string, I should be able it pass it to uploadactivity.java as such
private void upload(){
Intent upload = new Intent(ImagePreview.this, UploadActivity.class);
upload.putExtra("finalImage", filePath);
startActivity(upload);
}
and in uploadactivity.java
Intent upload = getIntent();
// image or video path that is captured in previous activity
finalImage = upload.getStringExtra("finalImage");
By this I can get to UpoloadActivity and upload button is displayed but filePath is not passed.
What am I doing wrong?
You're not using the same key for the extras:
upload.putExtra("finalImage", filePath);
upload.getStringExtra("filePath");
Replace
finalImage = upload.getStringExtra("filePath");
with
finalImage = upload.getStringExtra("finalImage");
how to pick images from gallery into an android application?
I need to browse images form my gallery and choose them for my app .how it can be done ?
You'll need to use an intent to the gallery (well, "a gallery").
You can find a code example here (I haven't done it but it looks like it will work):
http://www.androidsnippets.com/get-file-path-of-gallery-image
Use this Code:
fromGalleryButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//takePhotoFromGallery = true;// edited
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);//
startActivityForResult(Intent.createChooser(intent, "Select Picture"),10);
}
});
And then add this:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 10 && resultCode == Activity.RESULT_OK) {
Uri contentUri = data.getData();
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
imagePath = cursor.getString(column_index);
tempBitmap = BitmapFactory.decodeFile(imagePath); // this is your image
}
}
In Kotlin you can do this.
In onCreate method
binding.updatephoto.setOnClickListener {
contract.launch("image/*")
}
After onCreate
private val contract = registerForActivityResult(ActivityResultContracts.GetContent()){
it?.let {
val imageUri = it
}
}