I wanted to display my video thumbnail right after taking a video.
This is my current code in the onActivityResult method
if (requestCode == 101)
{
Bitmap bmThumbnail = ThumbnailUtils.createVideoThumbnail("/storage/emulated/0/myvideo.mp4", MediaStore.Images.Thumbnails.MICRO_KIND);
viewImage.setImageBitmap(bmThumbnail);
}
But it doesn't display anything on the image view.
Any workaround for this?
*UPDATE: FIXED
I've changed the code to:
if (requestCode == 101)
{
Bitmap bmThumbnail = ThumbnailUtils.createVideoThumbnail(f.getAbsolutePath().toString(), MediaStore.Images.Thumbnails.MINI_KIND);
viewImage.setImageBitmap(bmThumbnail);
}
The thumbnail is now showing and I have a bigger size, which is acceptable for viewing. Thanks to Sassa.
I just retrieved the absolute path of the file using f.GetAbsolutePath:
if (requestCode == 101)
{
Bitmap bmThumbnail = ThumbnailUtils.createVideoThumbnail(f.getAbsolutePath().toString(), MediaStore.Images.Thumbnails.MINI_KIND);
viewImage.setImageBitmap(bmThumbnail);
}
ThumbnailUtils.createVideoThumbnail will not work with all android OS hence you have to load ffmpeg library in your application to create thumbnail if you want to provide support for all android OS devices.
Thanks,
Devang
Related
I am developing an application that chooses an image of a wound and displays it on the application screen. with this, the user marks the region of interest of the wound, so that later the algorithm can recognize and process the region of interest. I'm doing this using the lib implementation 'com.github.gcacace: signature-pad: 1.2.1' to demarcate the region and then I'm saving the screen's "printscreen" so I can save the markup along with the image of the wound.
How I wish the image will look
Exit:
However, I want to cut the printscreen according to the image of the wound to send to the server to process the image. Can someone help me cut out the wound image after marking.
private fun saveImage(myBitmap: Bitmap?): String? {
try {
// image naming and path to include sd card appending name you choose for file
val mPath = Environment.getExternalStorageDirectory().toString() + "/imagesignature.jpg"
// create bitmap screen capture
val v1 = window.decorView.rootView
v1.isDrawingCacheEnabled = true
val bitmap = Bitmap.createBitmap(v1.drawingCache)
v1.isDrawingCacheEnabled = false
val imageFile = File(mPath)
val outputStream = FileOutputStream(imageFile)
val quality = 100
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream)
outputStream.flush()
outputStream.close()
//setting screenshot in imageview
val filePath = imageFile.path
val ssbitmap = BitmapFactory.decodeFile(imageFile.absolutePath)
imagem.setImageBitmap(ssbitmap)
} catch (e: Throwable) {
// Several error may come out with file handling or DOM
e.printStackTrace()
}
return ""
}
I am still a learner so for an easy way to crop an image I would suggest using this library:
https://github.com/ArthurHub/Android-Image-Cropper
This is where you can crop the image as per your requirement and store the image on the server
If you have the coordinates of the rectangle you want to save:
Bitmap croppedBmp = Bitmap.createBitmap(originalBmp, rectanglePositionX, rectanglePositionY, rectangleWidth, rectangleHeight);
Or you can try:
BitmapFactory.decodeStream(InputStream is, Rect outPadding, Options opts)
or
BitmapFactory.decodeFileDescriptor(FileDescriptor fd, Rect outPadding, Options opts)
where in the Rect outPadding you will set the coordinates of the rectangle you want to save.
As far as I know, I don't think it's possible to crop and image. In order to crop, you need to find the dimensions for the part that you want. I don't think you can tell the program the dimensions of what you want and then crop everything else off, as far as my knowledge goes. It might be possible to print an image, but I don't think Java can crop. Other coding programs might work better for this.
I am currently trying to reduce the quality of videos and audio before uploading to and online cloud database. Below is the code I have been using to record videos.
recordVideoIntent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0);
Changing the 0 to 1 in EXTRA_VIDEO_QUALITY will increase the quality and vice versa, but the file is still too large to download if it a 30 second or more video.
private void RecordVideoMode() {
Intent recordVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (recordVideoIntent.resolveActivity(getPackageManager()) != null) {
videoFile = createVideoFile();
// Continue only if the File was successfully created
if (videoFile != null) {
videoURI = FileProvider.getUriForFile(this,
"com.example.android.fileprovider",
videoFile);
recordVideoIntent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0);
recordVideoIntent.putExtra(MediaStore.EXTRA_OUTPUT, videoURI);
startActivityForResult(recordVideoIntent, REQUEST_VIDEO_CAPTURE);
}
}
}
Any help is very much appreciated!
You can go with this two methods :
Encode it to a lower bit rate and/or lower resolution. Have a look here:
Is it possible to compress video on Android?
Try to zip/compress it. Have a look here:
http://www.jondev.net/articles/Zipping_Files_with_Android_%28Programmatically%29
I have a list of images:
private int[] images = {
R.drawable.blue_icon_left_foot,
R.drawable.blue_icon_right_foot,
R.drawable.blue_icon_left_hand,
R.drawable.blue_icon_right_hand,
R.drawable.green_icon_left_foot,
R.drawable.green_icon_right_foot,
R.drawable.green_icon_left_hand,
R.drawable.green_icon_right_hand,
R.drawable.red_icon_left_foot,
R.drawable.red_icon_right_foot,
R.drawable.red_icon_left_hand,
R.drawable.red_icon_right_hand,
R.drawable.yellow_icon_left_foot,
R.drawable.yellow_icon_right_foot,
R.drawable.yellow_icon_left_hand,
R.drawable.yellow_icon_right_hand};
I want to let the user pick an image from the gallery by using an Intent:
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), 1);
One of the images of the array should then be replaced with the new image given by the user. When I get the image from the user, I only have a URI of the image:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == 1) {
Uri imageUri = data.getData();
//Insert image into images list
}
}
Is it possible to somehow get an integer ID of the image so that I can insert the image into the same list as the images from the drawable folder?
Or should I instead try to store a list of URI's (if it is possible to get URI's of the images from the drawable folder)?
Or is there a third solution that is completely different and much better?
You can generate ids with View.generateViewId() if you are using API 17 or larger.
From the main documentation:
Generate a value suitable for use in setId(int). This value will not
collide with ID values generated at build time by aapt for R.id.
Returns a generated ID value
You can check this answer to see what are the alternatives when you are using a lower API: Android: View.setID(int id) programmatically - how to avoid ID conflicts?
I think what you are trying to do is somewhat misguided. The resources in the /res directory are compile time resources that you bundle with your project. The image that a person selects via an intent from their device is a run time file that will vary by user, device, etc. You are better off not trying to treat them the same. Save the user selections as dataUris or strings and keep the resources as ints.
I have an ImageView that is not wanting to display a JPEG, but will display all the other PNG's in the ListView. I have tried things such as adding a transparent background, and nothing works. To get the data, I downloaded all of the images, and stored them into a blob in a SQLite database. I retrieved them by fetching the info from the database, and according to the database, their is binary JPEG data available.
Here is the image I am trying to display (I have it already downloaded in binary data): http://image-store.slidesharecdn.com/1a4f1444-02e2-11e4-9166-22000a901256-large.jpeg
Here is the code I am using to try and convert the binary data back to a Bitmap:
try
{
String profilePictureBytes = submittedImageTemp; // this string holds the binary jpeg, png data, etc.
byte[] encoded;
try
{
encoded = profilePictureBytes.getBytes("ISO-8859-1");
ByteArrayInputStream imageStream = new ByteArrayInputStream(encoded);
Bitmap theBitmap = BitmapFactory.decodeStream(imageStream);
attachPreviewOne.setBackgroundColor(Color.TRANSPARENT);
attachPreviewOne.setImageBitmap(theBitmap);
}
catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
}
A. Extract your jpeg from the blob to a temp file. You might want to use the getCacheDir() folder for that
B. Load it from temp file:
BitmapFactory.Options options = new BitmapFactory.Options();
Bitmap bm = BitmapFactory.decodeFile(jpegTemFile, options); //<------------
imageView.setImageBitmap(bm);
The above should work fine. Still - few things you might want to consider:
C. Depending on your jpeg, the loading operation - decodeFile - might be lengthy. You might want to do it in an
AsyncTask.
D. You might also want to consider setting a sampleSize so to reduce size of loaded image:
options.inSampleSize = 2; // setting to N will reduce in-memory size by factor of N*N
Or, you might use Picasso for the job. I used it in several projects and was happy with what I got:
Picasso.with(context)
.load(url)
.resize(50, 50)
.centerCrop()
.into(imageView)
I continuously get OutOfMemory exceptions trying to decode an image from camera in my Android app. There are many questions dealing with the problem, but my case is especially weird because I get the exception even when just trying to get the bounds with options.inJustDecodeBounds=true.
Here's the code that starts the camera:
protected void takePicture() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File image = new File(IMAGE_PATH, "camera.jpg");
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(image));
startActivityForResult(takePictureIntent, 0);
}
Here's the code that triggers the exception:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK ) {
String rawImageName = new String(IMAGE_PATH + "/camera.jpg");
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(rawImageName); // The exception is thrown here
.
.
.
}
}
I tried to decode the image using a very high sampling rate, but still I get the same exception:
options.inSampleSize = 20;
options.inJustDecodeBounds = false;
options.inPreferredConfig = Bitmap.Config.RGB_565;
Bitmap photo = BitmapFactory.decodeFile(rawImageName); // Again the exception
Except for that, the application seems to run correctly and there is enough free memory. I can open correctly the image in the gallery app. Moving the image to a different directory didn't help. Any ideas what could cause it? What could possibly cause the exception while decoding with inJustDecodeBounds = true?
You need to pass the options to the decode call:
BitmapFactory.decodeFile(rawImageName, options);
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = 4; // 1/4
o2.inPurgeable = true;
Bitmap b=BitmapFactory.decodeFile(imagePath,o2);
try this. and also resize your image and make bitmap objects null after use.
give call to System.gc(); it doesn call gc but it gives hint.
also dont make lots of bitmap objects. reuse the same bitmap object and make it null after use.