in my app I use SqLite Database "Templates", where I have table "favourites", constains of four columns "id", "description", "imagePath", "category". I would like user to be able use their own images in my app, like this:
When user click button, he choose image from gallery, write description and choose category of image. I know I need use Content Values to do this. But how can I do my table has imagePathes, i.e how can I show them later using RecyclerView and Picasso?
You can load image from device memory:
Picasso.get().load(new File("path-to-you-image/image.png")).into(imageView);
If you are asking "how to get image path" - you can get it in the onActivityResult method:
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]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
Related
I am new to android (started this week) and am currently writing a photo album app for a class. I am currently trying to get an image selected by the user, however, after selecting the image I am using cursor to get the path but the getString() method is returning null. Here is the code segment in onActivityResult:
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndexOrThrow(filePathColumn[0]);
final String picturePath = cursor.getString(columnIndex);
cursor.close();
//create photo object
I used the debugger tool to step through the code. data.getData() does not return null.filePathColumn[0] contains "_data", crusor.moveToFirst() returns true and columnIndex = 0. Then the cursore.getString(columnIndex) returns null. Can anybody help me figure out what is going wrong ?
please check image hereI need to set last taken image form gallery in image view or on Image button automatically just like used in default camera ,some body help me to sort out this problem
We can get the last taken image from MediaStore,
String[] proj = new String[]{
MediaStore.Images.ImageColumns.DATA,
};
final Cursor cursor = getContentResolver()
.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, proj, null,
null, MediaStore.Images.ImageColumns.DATE_TAKEN + " DESC");
if (cursor.moveToFirst()) {
String lastImageLocation = cursor.getString(0);
}
I have the codes work for loading images from the gallery but I really do not understand how it works. Here are the codes.
#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) { //Browse Gallery is requested
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]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
loadImage(picturePath); //load picture according the path
image_View.setImageBitmap(pic); //Show the selected picture
}
}
Uri selectedImage = data.getData();
Get the uri of selected image from intent
String[] filePathColumn = { MediaStore.Images.Media.DATA };
MediaStore.Images.Media.DATA is constant. I do not understand why do not use String instead of String[]
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
I do not understand this line.
cursor.moveToFirst();
Move to the first picture in Gallery.
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
For this one, I always get 0 no matter which picture I choose.
String picturePath = cursor.getString(columnIndex);
Since columnIndex is always 0, then how can it get a different path for different pictures?
Can anyone help me to check whether my explanation is correct and explain the line that I do not understand? Thanks.
1-
Uri selectedImage = data.getData();
This is the statement where you need to read the data passed through another intent which you called earlier via startActivityForResult method. In this case probably you open an intent and let the user select an image, then the URI of the image will be returned to you and you use getData to read that.
2-
String[] filePathColumn = { MediaStore.Images.Media.DATA };
When you want cursors to read something a Content Provider (via ContentResolver) you need to specify which columns you need to read from the database, and the argument you need to pass should be an array of String (whether it has one or more columns you still need to pass an array). MediaStore.Images.Media is a Database Contract which contains constants which you need to use to talk to Content Providers
3-
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
Cursors are used for reading data from Content Providers. If you are familiar with other programming languages it is like reading rows from database and your results are stored in Cursors. When you pass URI, you don't need to specify which database to read, ContentResolver will find that out for you (this is an advantage of using content providers)
4-
cursor.moveToFirst();
When you read the desired rows from the database (in this case probably you just selected one image), you need to move the cursor to point to the first entry (row) of the returned results
5-
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
You need to know which column you need to access to read your desired data (in this case file path name). so you ask cursor what is the column index of the file pathname and it will return you the column index. and of course in this case it will be always 0 because you only asked the content provider to return one column (file pathname) so there would be no more data to show other than that
6-
String picturePath = cursor.getString(columnIndex);
and finally this statement asks cursor to get the file pathname located at the index (in this case index 0) so at the end you have your path to file. Note that you can read only one picture data at a time with this method
Cursors store query result records in rows and grant many methods to access and iterate through the records. Also int columnIndex = cursor.getColumnIndex(filePathColumn[0]); is the reason why you always get 0 for your index
I've developed image cropper so that user can crop selected image from gallery. When user has taken a picture with camera or has selected it from gallery, image uri is queried for image metadata and image file path is passed to various bitmap methods for bitmap creation.
But now I've expanded users selection option to Dropbox also and uri that I receive from intent is in the form of:
01-08 09:11:21.051: I/DROPBOX(2761): file:///storage/emulated/0/Android/data/com.dropbox.android/files/u98667695/scratch/Capture.PNG
Query on this uri returns null for cursor, so I tried to convert uri to string, remove first 7 characters and pass that string as image file path. This works fine if I select image from root directory on Dropbox...
So, my question how image request from external sources like Dropbox for instance in order to get image file path when intent returns uri for requested image?
This is method where I'm retrieving file path:
private void getImageData(Uri selectedImage){
String[] filePathColumn = { MediaStore.Images.Media.DATA };
String[] fileOrientationColumn = { MediaStore.Images.Media.ORIENTATION };
Cursor cursor = context.getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
/* if(cursor == null){
imageFilePath = selectedImage.toString().substring(6);
return;
}
*/ cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
imageFilePath = cursor.getString(columnIndex);
Log.i("CURSOR path", imageFilePath);
cursor = context.getContentResolver().query(selectedImage,
fileOrientationColumn, null, null, null);
cursor.moveToFirst();
columnIndex = cursor.getColumnIndex(fileOrientationColumn[0]);
String fileOrientation = cursor.getString(columnIndex);
// Izmena
if(fileOrientation == null)fileOrientation = "0";
imageOrientation = Integer.parseInt(fileOrientation);
cursor.close();
Log.i("CURSOR orientation", fileOrientation);
}
In above case you can do the following.
String filePath="I/DROPBOX(2761): file:///storage/emulated/0/Android/data/com.dropbox.android/files/u98667695/scratch/Capture.PNG";
int firstIndex=filePath.indexOf("file");
String actualPath=filePath.subString(firstIndex);
So the actualPath contains required path as follows, irrespective of root directory or file location.
actualPath="file:///storage/emulated/0/Android/data/com.dropbox.android/files/u98667695/scratch/Capture.PNG"
In my application, I open my gallery and pick a picture and then show my image in an image view then I'm supposed to send it through bluetooth.
That's what i've reached :
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]);
String filePath = cursor.getString(columnIndex);
cursor.close();
Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
iv.setImageBitmap(yourSelectedImage);
How to send it through bluetooth?
N.B I've opened my bluetooth and made it also discover-able by other devices .
Please I'll be thankful if u helped me in my assignment =]