Getting a file's location in android studio - java

Im trying to pick a file in a file explorer using intent.
here is my code:
Intent intent = new Intent();
intent.setAction(intent.ACTION_GET_CONTENT);
intent.setType("*/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(Intent.createChooser(intent, "Select a File to Upload"), FILE_SELECT_CODE );
it works fine and picks a file but I need to get the picked file's location. what should I do?

Once you select a file this method will be called automatically.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
Uri filePath = data.getData();
}
}

Try this
It will give you the bitmap
Bitmap bm = null;
if (data != null) {
try {
bm = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), data.getData());
} catch (IOException e) {
e.printStackTrace();
}
}
and here data.getData() gives you the uri

Related

Android studio, pick picture form files, pictures are grey out

I have managed to get some code working for picking a pictures from mobile stored files to the imageView, but I am not sure why all the images files are grey out, and I cannot select them.
I have searched for a solution, but I couldn't find anything, so any advice will be appreciated.
methods for selecting a picture:
public void chooseImage (View objectView){
try {
Intent objectIntent = new Intent();
objectIntent.setType("iv_addBday/*");
objectIntent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(objectIntent, PICK_IMAGE_REQUEST);
}
catch (Exception e){
}
}
protected void onActivityResult (int requestCode, int resultCode, #Nullable Intent data){
try {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null){
imageFilePath = data.getData();
imageToStore = MediaStore.Images.Media.getBitmap(getContentResolver(), imageFilePath);
imageView.setImageBitmap(imageToStore);
}
} catch (IOException e) {
e.printStackTrace();
}
}
example
What about :
public void chooseImage (View objectView){
try {
Intent objectIntent = new Intent();
objectIntent.setType("image/*");
objectIntent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(objectIntent, PICK_IMAGE_REQUEST);
}
catch (Exception e){
}
}

android take image from phone album and store it in sqlite database

i'm making an app where the user can choose images from the phone's album and store it into a table in sqlite database, i have seen many posts here about this issue but couldn't understand the solutions (neither did they work for me), in my activity i have an imageview which when clicked will open the album and allow the user to choose an image and also i have a button which when clicked will store the image in sqlite database, i'm just able to choose the image but after that i'm stuck, i got to this method in my code:
public void getImage(View view) throws IOException {
ImageView v=(ImageView)view;
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent,1);
}
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
if (resultCode == RESULT_OK) {
try {
final Uri imageUri = data.getData();
final InputStream imageStream = getContext().getContentResolver().openInputStream(imageUri);
final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
image.setImageBitmap(selectedImage);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
now what should i do after this to store the image in the db?
If you are sure about the images will not be removed from internal storage, you can save the image path. If want to save the image data, you can do the following.
for selecting image
private void selectImage() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, IMAGE_REQ);
}
In onActivityResult, from Intent data ...
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == IMAGE_REQ && resultCode == Activity.RESULT_OK && data != null) {
Uri path = data.getData();
try {
bitmap = MediaStore.Images.Media.getBitmap(getContext().getContentResolver(), path);
Bitmap.createScaledBitmap(bitmap, 150, 150, true);
} catch (IOException e) {
e.printStackTrace();
}
}
}
get byte[] from bitmap
public static byte[] getByteArrayFromBitmap(Bitmap bitmap) {
if(bitmap == null) return null;
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 50, byteArrayOutputStream);
return byteArrayOutputStream.toByteArray();
}
then save the byte[] as blob in sqlite
for getting the images as bitmap
public static Bitmap getBitmapFromByteArray(byte[] blob){
if(blob == null) return null;
Bitmap bitmap = BitmapFactory.decodeByteArray(blob, 0, blob.length);
return bitmap;
}

Uri to Bitmap Then Transfer Another Activity

I have been trying to provide bitmap through uri after that provided bitmap transfer another activity how can I do it??
I find a few ways to convert uri I was unsuccessful
Uri imageUri = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(),imageUri);
picture.setImageBitmap(bitmap);
}
catch (IOException e) {
e.printStackTrace();
}
I try to implement that codes I was in failure
Uri IMAGE_URI = imageReturned
InputStream image_stream = getContentResolver().openInputStream(IMAGE_URI);
Bitmap bitmap= BitmapFactory.decodeStream(image_stream );
picture.setImageBitmap(bitmap);
That is another way which my tried code
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (resultCode == RESULT_OK) {
Uri resultUri = result.getUri();
picture.setImageURI(resultUri);
}
else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
Exception error = result.getError();
Toast.makeText(this, "Error Merroor", Toast.LENGTH_SHORT).show();
}
}
}

How to add image in Sqlite database from phone gallery?

This is my code to get data from gallery and save it in SQLite database but its not working
.....
case PICK_FROM_GALLERY:
Bundle extras2 = data.getExtras();
if (extras2 != null) {
Bitmap yourImage = extras2.getParcelable("data");
// convert bitmap to byte
ByteArrayOutputStream stream = new ByteArrayOutputStream();
yourImage.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte imageInByte[] = stream.toByteArray();
Log.e("output before conversion", imageInByte.toString());
// Inserting Contacts
Log.d("Insert: ", "Inserting ..");
db.addContact(new Contact(resultCode, "Android", imageInByte));
Intent i = new Intent(Editor.this, Editor.class);
startActivity(i);
finish();
}
break;
}
}
Store Uri of an Image, through Uri you can set an Image in ImageView
Uri imageUri;
Intent pickUpImage = new Intent(Intent.ACTION_PICK);
pickUpImage.setType("image/*");
startActivityForResult(pickUpImage, 1);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == Activity.RESULT_OK) {
Uri uri = data.getData();
imageUri = uri;
profilePic.setImageURI(imageUri);
}
}

How to open Gallery to pick an image?

I am looking for a way to open onClick the gallery to pick an Picture.
I want to use that local "uploaded" picture in a listview of pictures.
Can someone help and explain me how to do that. I am a beginner in Android Developing.
public void pickImage() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, PICK_PHOTO_FOR_AVATAR);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_PHOTO_FOR_AVATAR && resultCode == Activity.RESULT_OK) {
if (data == null) {
//Display an error
return;
}
InputStream inputStream = context.getContentResolver().openInputStream(data.getData());
//Now you can do whatever you want with your inpustream, save it as file, upload to a server, decode a bitmap...
}
}
Here is a code i used in my app
Intent pickImageIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(pickImageIntent, PICKIMAGE_REQUESTCODE);
And at the onActivity result method
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICKIMAGE_REQUESTCODE) {
if (resultCode == RESULT_OK) {
Uri imageUri = data.getData();
InputStream inputStream = null;
try {
inputStream = getContentResolver().openInputStream(imageUri);
bitmap = BitmapFactory.decodeStream(inputStream);
}
catch (FileNotFoundException e)
{
e.printStackTrace();
} finally {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
Log.d(TAG, "onActivityResult: error closing InputStream during reading image from the phone external storage");
}
}
}

Categories