I currently trying to carry out the same operation (OCR) on images in android, one option is to use the camera and the other is to load an image from the SD-CARD. The code works when taken from a camera where the code is
Bitmap bitmap = BitmapFactory.decodeFile(_path,options);
where _path is equal to the last image taken using the application ( _path = DATA_PATH + "ocr.jpg"; ) . However when I try to use an image selected from the gallery where _path would equal,
imageCaptureUri = data.getData();
_path = imageCaptureUri.getPath();
The program locks up with the error
Failure deleiving result ResultInfo{who=null, request = 2, result = -1, data=intent {dat=content://media/external/images/media/26 typ=image/jpeg(has extras)}} to activity{com.project.projectActivity}: java.lang.NullPointerException
If anybody has an idea of whats going on I'd like to hear from you !!
You can get path of the image as..
_path = getPath(imageCaptureUri);
public String getPath(Uri uri) {
String[] projection = { MediaColumns.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
column_index = cursor
.getColumnIndexOrThrow(MediaColumns.DATA);
cursor.moveToFirst();
imagePath = cursor.getString(column_index);
return cursor.getString(column_index);
}
and then
Bitmap bitmap = BitmapFactory.decodeFile(_path,options);
Note : If you are cropping image then this method does not work in this case Gallery will generate cropped image on same directory of image which you are cropping.
The Gallery returns you an Uri to access the image.
You need to use the decodeStream method from BitmapFactory, and for that you need to open an InputStream on the Uri given :
InputStream is = getContentResolver().openInputStream(imageCaptureUri);
Bitmap bitmap = BitmapFactory.decodeStream(is, options);
Related
I want thumbnail from a video at any specific position. I am using ThumbnailUtils in order to get thumbnail from video uri and assigning to bitmap but I am getting null value on bitmap.
Any reasons how this is happening and how do I fix this?
selectedVideoUri = data.getData();
bitmap = ThumbnailUtils.createVideoThumbnail(getRealPathFromURI(videoUri),
MediaStore.Images.Thumbnails.MINI_KIND);
public String getRealPathFromURI(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;
}
You can use Glide to load thumb directly to imageview
Glide.with(activity).load(videoPath).into(imageview);
First Load Video List with its path in Your array list using below method
private void loadData(String currentAppPath) {
hiddenpath = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + currentAppPath);
String[] fileName = hiddenpath.list();
try{
for(String f : fileName){
if(HelpperMethods.isVideo(f)){
videoFiles.add(hiddenpath.getAbsolutePath()+"/"+f);
}
}
new Loader().loadImages(Environment.getExternalStorageState());
}catch (Exception e){
}
}
You need Loader().loadImages method so i declare this method in separate class file. see below code
public class Loader {
String[] imagieFiles;
public void loadImages(String path){
Log.e("path",path);
System.out.println(path);
} }
Then after You can use below Code to Get Video Thumbnail. By default Each Video Store two size Thumbnail.
1) MINI -- MediaStore.Images.Thumbnails.MINI_KIND and
2) MICRO -- MediaStore.Images.Thumbnails.MICRO_KIND
Bitmap thumb = ThumbnailUtils.createVideoThumbnail(filePath,
MediaStore.Images.Thumbnails.MINI_KIND);
BitmapDrawable bitmapDrawable = new BitmapDrawable(thumb);
contentViewHolder.videoView.setImageBitmap(thumb);
This is supported by Android natively using MediaPlayer SeekTo method
If you just want to show the video placeholder to display then you can use below code:
video_view.setVideoPath(videoPath);
video_view.seekTo(3000); // in milliseconds i.e. 3 seconds
ThumbnailUtils returns null when file or video is corrupted.
but I wanted to only use Uri and this is a good solution to do this:
val mmr = MediaMetadataRetriever()
mmr.setDataSource(videoUri)
val thummbnailBitmap = mmr.frameAtTime
imageView.setImageBitmap(thummbnailBitmap)
I'm trying to gather EXIF info (date of picture taken, geotagging, orientation) from images selected from an EXTERNAL_CONTENT_URI intent, but it seems that if the pictures come from the internet (e.g. Google Photos) the EXIF data is somehow truncated.
For example if I download a picture from a web browser from photos.google.com on my PC, its size is 4.377.104 bytes, and all the EXIF data are there.
While if I download the same exact image using the command:
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
its size is 4.363.578 bytes (13526 bytes less than the original) and all the EXIF data are lost
Any idea on how to download the full original image?
PS: if I select a picture from the gallery which was taken from the phone and it is still resident on the phone's storage, the EXIF data are present
I finally found out a solution. after getting the source image Uri in the onActivityResult method using
Uri selectedImage = data.getData();
I use the following function to get the necessary data from the picture
public static ImageInfo getImageInfo(Context context, Uri photoUri) {
Cursor cursor = context.getContentResolver().query(photoUri,
new String[] {
MediaStore.Images.ImageColumns.ORIENTATION,
MediaStore.Images.ImageColumns.LATITUDE,
MediaStore.Images.ImageColumns.LONGITUDE,
MediaStore.Images.ImageColumns.DATE_TAKEN } , null, null, null);
if (cursor.getCount() != 1) {
return null;
}
cursor.moveToFirst();
ImageInfo i = new ImageInfo();
i.Orientation = cursor.getInt(0);
i.Lat = cursor.getDouble(1);
i.Lon = cursor.getDouble(2);
i.DateTakenUTC = cursor.getLong(3)/1000;
cursor.close();
return i;
}
How can I load an image from sdcard to canvas. I tried this code and it is not working.
Uri selectedImage = data.getData();
if (selectedImage == null) {
Log.e(getString(R.string.app_name), "selected image uri is null");
return;
}
String[] filePath = { MediaStore.Images.Media.DATA };
Cursor cursor = getActivity().getContentResolver().query(selectedImage, filePath, null, null, null);
if(cursor==null){
Log.e(getString(R.string.app_name), "image cursor is null");
return;
}
cursor.moveToFirst();
String imagePath = cursor.getString(cursor.getColumnIndex(filePath[0]));
cursor.close();
Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
Bitmap bitmap2 = Bitmap.createBitmap(bitmap.getHeight(),bitmap.getWidth(),bitmap.getConfig());
mCanvas.setBitmap(bitmap2);
Paint p = new Paint();
mCanvas.drawBitmap(bitmap,0,0,p);
What is wrong in this code?
I do not know where this Uri is coming from. However:
Get rid of all the MediaStore stuff
Use ContentResolver and openInputStream() to get an InputStream on the content identified by the Uri
Use BitmapFactory.decodeStream() to decode from that stream
Also:
Make sure that you request READ_EXTERNAL_STORAGE or WRITE_EXTERNAL_STORAGE, including handling runtime permissions
Eventually, move the openInputStream() and decodeStream() work to a background thread
Consider switching to an image-loading library
When I get bitmap for mp4 file with: ThumbnailUtils.createVideoThumbnail(mediaFile.getAbsolutePath(), MediaStore.Video.Thumbnails.MINI_KIND); return null
Try this, May be you media file path was wrong. Use below method you will get exact path. Its working for me
Bitmap thumb = ThumbnailUtils.createVideoThumbnail(getPath(outputFileUri),
MediaStore.Images.Thumbnails.MINI_KIND);
photo_Img.setImageBitmap(thumb);
/**
* Get file path
*/
public static String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = context.managedQuery(uri, projection, null, null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
I think you must set requires permission in AndroidManifest <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
File file = new File(filepath);
Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(file.getAbsolutePath(), MediaStore.Video.Thumbnails.FULL_SCREEN_KIND);
From android docs:
public static Bitmap createVideoThumbnail (String filePath, int kind)
Create a video thumbnail for a video. May return null if the video is corrupt or the format is not supported.
Hence, I guess you need to re-check the mp4 file.
I am having a problem capturing an image from the built-in camera app on the Samsung Galaxy S.
I have a button on my app that when pressed launches the camera:
ContentValues values = new ContentValues();
values.put(Images.Media.MIME_TYPE, "image/jpeg");
mPicUri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values);
intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, mPicUri.getPath());
startActivityForResult(intent, REQ_CODE_TAKE_PIC);
From what I read on the interwebz, I can get the full-sized just-taken picture using the URI I passed to the intent. And so I have this on my onActivityResult:
Uri selectedImage = mPicUri;
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();
And then using the variable filePath, I set the image in an ImageView. I am getting no image, and when I stepped through the code, I found out that BitmapFactory.decodeFile() is returning null.
Bitmap chosenPic = BitmapFactory.decodeFile(filePath);
So I tried a little more debugging. I found out that mPicUri returns a seemingly valid URI, such as: content://media/external/images/media/90. After the picture is taken and the user chooses to save the picture, the cursor resolves to the following filePath: /sdcard/DCIM/Camera/1285601413961.jpg. No bitmap is decoded though, BUT when I looked through the gallery, the picture I just took is there. So I tried to take a look at the URI of that picture, and this is what I got:
URI is: content://media/external/images/media/91
File path is: /sdcard/DCIM/Camera/2010-09-27 23.30.30.jpg
And so, it looks like the value I got from mPicUri is not the final URI that will be used.
Am I missing a step here? All I really want to do is retrieve the file of the just-taken picture.
Thanks in advance,
Zarah.
Did you try to specify File in both cases before taking picture and the same after?
public static File getTempFile() {
//it will return /sdcard/image.jpg
final File path = new File(Environment.getExternalStorageDirectory(), "MyApp");
if (!path.exists()) {
path.mkdir();
}
return new File(path, "image.jpg");
}
I'm using this function to get and set file path.