I'm trying to set the Image to the ImageView through calling a Bitmap but suddenly the line below shows Cannot resolve putExtra. I think there is updated syntax of this line. Is somebody knows the problem about this? I search many times but this line must be required
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 100){
Bitmap bitmap = (Bitmap) data.putExtra().get("BitmapImage"); //Error
PsId.setImageBitmap(bitmap);
}
}
you want to GET data, not PUT... just use getExtras() instead of putExtra()...
Bitmap bitmap = (Bitmap) data.getExtras().get("BitmapImage");
btw. avoid passing Bitmaps in Intent/Bundle, these are too heavy and may result in some rare crashes related to memory handling by OS... it would be better to store this bitmap in memory or cache and pass as result only reference to it (e.g. filename or some key under bitmap was stored)
Related
So I'm making a list app for wine where you can put in details and an image about wines that you enter and put the data into an SQLite database on the phone.
I just worked out getting the image into the database but it's very low quality, even though I have the quality integer set to 95 (out of 100).
Can anyone give tips on how to do this in a better way?
Here is the relevant info from the AddActivity:
btnAddPicture.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, 1888);
}
});
//this method happens after taking the image
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if (requestCode == 1888 && resultCode == RESULT_OK){
toastMessage("Image has been taken successfully");
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
imageBitmap.compress(Bitmap.CompressFormat.JPEG, 95, stream);
imageInByte = stream.toByteArray();
}
}
I then put that imageInByte variable into the database.
The reason that the image is such low quality is that it is actually getting the thumbnail version of the picture.
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
This doesn't retrieve the full image. If you want the full image, you need to use the EXTRA_OUTPUT extra to save the full image. Then, you can put the file path into the database to retrieve the full sized image.
For documentation on ACTION_IMAGE_CAPTURE:
https://developer.android.com/reference/android/provider/MediaStore.html#ACTION_IMAGE_CAPTURE
Hope this helps anyone else!
I am using onActivityResult to get an image taken from the camera intent. I would like to get the actual image and not the thumbnail. How can I do that. When I use data.getExtras().get("data") I get the thumbnail which is low quality. I do not wish to save the image locally as I will be uploading it to a server.
Camera Intent:
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
onActivityResult:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == Activity.RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
}
}
How can I do that
Provide a Uri in EXTRA_OUTPUT in your Intent, pointing to a location where the third-party camera app can write the image.
I do not wish to save the image locally as I will be uploading it to a server.
Then either settle for the thumbnail or write your own camera code (rather than using a third-party app).
I take a picture in Android via
Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePicture, CAMERA_REQUEST);
and show / save it via
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
ImageView theImage = (ImageView) findViewById(R.id.preview);
theImage.setImageBitmap(photo);
// try to save its
try {
File testFile = new File(Environment.getExternalStorageDirectory(), "test.png");
testFile.createNewFile();
FileOutputStream out = new FileOutputStream(testFile);
photo.compress(Bitmap.CompressFormat.PNG, 90, out);
} catch (Exception e) {
e.printStackTrace();
}
This works fine, however the quality of the image is very bad. I do not know why, since I take the picture with 8 mega pixels.
Is there a way to do this without requiring the camera manually?
Take a closer look at this post: there are two ways to capture an image in Android. First one is designed for taking small and lightweight pictures - that's the approach you use, and the second one captures full-sized pictures and writes them to storage. The post describes both ways of accomplishing this task.
I am trying to respond to a result from an activity using startActivityForResult. I am getting the expected result but am unsure how to respond to the result. What I'd like to do is call a different ArrayList (which is a resource) depending on the result.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
Bundle d = data.getExtras();
String oil = d.getString("oil");
Resources res = getResources();
String[] oil_info = res.getStringArray(R.array.OIL);
...
}
}
I'd like R.array.OIL to change in response to the result. i.e. if oil = 'BASIL' change R.array.OIL to R.array.BASIL. I thought about making a hashmap but couldn't figure out how to put R.array.OIL into a hashmap as you cannot put primitives in a hashmap. I am pretty new to android and java so I'm sure there is a better way to do this. Any help would be much appreciated. Thanks.
Use getIdentifier() for your requirement as below.
e.g. in array type 2 items are there as OIL and BASIL with different values.
you can access like R.array.OIL and R.array.BASIL.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
Bundle d = data.getExtras();
String oil = d.getString("oil");
Resources res = getResources();
int id = res.getIdentifier(oil, "array", getPackageName());
String[] oil_info = res.getStringArray(id);
...
}
}
In above code if string oil is "OIL" then id will become R.array.OIL and oil is "BASIL" then id will become R.array.BASIL
what you are trying is actually an attempt of permanent storage . this is not allowed for "Res" folder or "R.smthing.smthing" data's .
use shared preferences for primitive storage .
see whether you can save array through sharedPreferences else use Serialization or SQLite database .
I am attempting to launch the built-in camera to take a picture, a picture that will have a name specified by the activity launching the camera. (code below)
When the camera returns, onActivityResult() goes straight to resultCode == Activity.RESULT_CANCELED. Any explanation for this and solutions would be greatly appreciated.
The camera indeed does take the image, I can see it in my sdcard with a file viewer, but its name is the stock one from the camera. How can I get the name of this taken image to be the one supplied by the activity?
Camera intent code
Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File image = new File("Team image.jpg");
camera.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
camera.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(image));
camera.putExtra(MediaStore.Images.Media.TITLE, "Team image");
startActivityForResult(camera, PICTURE_RESULT);
activityresult code
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if(requestCode == PICTURE_RESULT){
if(resultCode == Activity.RESULT_OK) {
if(data!=null){
Bitmap image = BitmapFactory.decodeFile(data.getExtras().get(MediaStore.Images.Media.TITLE).toString());
grid.add(image);
images.addItem(image);
}
if(data==null){
Toast.makeText(Team_Viewer.this, "no data.", Toast.LENGTH_SHORT).show();
}
}
else if(resultCode == Activity.RESULT_CANCELED) {
Toast.makeText(Team_Viewer.this, "Picture could not be taken.", Toast.LENGTH_SHORT).show();
}
}
}
Have you mark the launch mode of your activity as "singleInstance"?
That may cause your first problem.
My camera goes normal when I remove the "singleInstance".
The two issues are likely related, having to do with the way you are creating the file reference that passes to the camera. If you want your image file to save to the SD Card, you need to create a file reference that includes a full-path to that location, not just a filename. For example, this code would save the image file on the SD card root:
Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File image = new File(Environment.getExternalStorageDirectory(),"TeamImage.jpg");
camera.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(image));
startActivityForResult(camera, PICTURE_RESULT);
I also changed your filename to not include a space; only because I'm not certain that the Camera application won't blow up on that piece also. Since the Camera is getting confused trying to open and write to your file location, that is likely why you always return with RESULT_CANCELED. You don't need the WRITE_EXTERNAL_STORAGE permission here, since the Camera app is doing the SD Card access.
One more note: I don't believe other MediaStore extras can be passed with this Intent. Typically, if you want metadata to be attached to your image, you have to insert the Uri reference with that metadata into the MediaStore ContentProvider prior to saving the image to disk.
Hope that helps!
Not sure what's wrong with your code, here's what works for me:
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA_PIC_REQUEST);
and
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
switch (requestCode) {
case CAMERA_PIC_REQUEST:
Bitmap b = (Bitmap) data.getExtras().get("data");
if (b != null) {
updateThumbnail(b);
if (mBitmap != b) {
b.recycle();
}
}
break;
}
}