I have an ImageView after getting id from xml.as
on ImageView we set clicklistener which open gallery and camera opetion you can set image from camera as well gallery
ain #2
profileimage = (ImageView) findViewById(R.id.profileimage);
profileimage.setBackgroundResource(R.drawable.no_img);
protected void onActivityResult(int requestCode, int resultCode,
Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (resultCode == RESULT_CANCELED) {
// TODO
return;
}
Log.e("request code", "1:" + requestCode);
switch (requestCode) {
case CAMERA_SELECT:
Log.e("in camera select", "1");
// Get the camera data
cameracalling(intent);
break;
case Gallery_Select:
ongallerycalling(intent,resultCode);
}
}
private void cameracalling(Intent intent){
Bitmap photo = (Bitmap) intent.getExtras().get("data");
profileimage.setImageBitmap(photo);
}
profileimage.buildDrawingCache();
Bitmap bmap = profileimage.getDrawingCache();
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bmap.compress(Bitmap.CompressFormat.JPEG, 90, bao);
byte [] ba = bao.toByteArray();
bitmapString=Base64.encodeBytes(ba);
Drawable draw = LoadImageFromWebOperations("" + objUserInformationSitesList.getProfileImage());
profileimage.setBackgroundDrawable(draw);
We are sending bitmap string to server image upload on server properly but when we open next time this screen that webservice call on which we upload image which will give all data (actually this user profile screen) .when we set server image then default image also set on background
objUserInformationSitesList this object which contains all information after parsing the web service. behind profile image ,default image also looking which set by me on number #1
if I unable to explain properly then please tell me.
use in onclicklistener
profileimage.setBackgroundResource(0);
You should replace below line:
profileimage.setBackgroundResource(R.drawable.no_img);
with this line:
profileimage.setImageResource(R.drawable.no_img);
You set the image as the background of drawable and later you set bitmap as image source. So imageview background doesn't change. You should set image as image resource.
Related
I know this is quite common to ask but I'm really confused this time. All I need is to save the BLOB image to the SQLite and I have already done and saved it to SQLite, actually, I have two images saved as BLOB in SQLite I compared them but I really don't know the difference the other one contains 3000X4000 pixels 2.72 MIB which much heavier size and the other one is much lighter which is 182x250 50.81Kib and this is what I really want the lightier size. I upload two images to see what's really happening
Actually, I've been trying to develop Text Image Recognition. The image which bigger size sample, after capturing an image it should be crop and saves to SQLite while the lesser size just directly captured image and save to SQLite now they have the save function to save to SQLite I really don't know why they have different size and I think the problem is on the crop or Pickcamera()method which it changes the size of the image? is that possible?.
The bigger one
The lesser one
The bigger size code Image after clicking button it goes to pickCamera() method and the image display to the another activity
public void pickCamera() {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, "NewPic");
values.put(MediaStore.Images.Media.DESCRIPTION, "Image to Text");
image_uri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,values);
Intent cameraIntent = new Intent (MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, image_uri);
startActivityForResult(cameraIntent, IMAGE_PICK_CAMERA_CODE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK){
if (requestCode == IMAGE_PICK_GALLER_CODE){
CropImage.activity(data.getData()).setGuidelines(CropImageView.Guidelines.ON).start(this);
}
if (requestCode == IMAGE_PICK_CAMERA_CODE){
CropImage.activity(image_uri).setGuidelines(CropImageView.Guidelines.ON).start(this);
}
}
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE){
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if(resultCode ==RESULT_OK){
Uri resultUri = result.getUri();
resultUri.getPath();
BitmapDrawable bitmapDrawable = (BitmapDrawable)mPreviewIv.getDrawable();
Bitmap bitmap = bitmapDrawable.getBitmap();
TextRecognizer recognizer = new TextRecognizer.Builder(getApplicationContext()).build();
if(!recognizer.isOperational()){
Toast.makeText(this,"Error",Toast.LENGTH_SHORT).show();
}
else{
Frame frame = new Frame.Builder().setBitmap(bitmap).build();
SparseArray<TextBlock> items = recognizer.detect(frame);
StringBuilder sb = new StringBuilder();
for (int i = 0; i<items.size(); i++){
TextBlock myItem = items.valueAt(i);
sb.append(myItem.getValue());
sb.append("\n");
}
Intent i = new Intent(ScanCashCard.this, ScannedDetails.class);
//camera I want to display the image view to another Activity and save to SQLite
i.putExtra("CashCardImage",image_uri.toString());
startActivity(i);
}
}
}
}
Display to another activity
String resultUri = extras.getString("CashCardImage");
Uri myUri = Uri.parse(resultUri);
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(),myUri);
mPreviewCashCard.setImageBitmap(bitmap);
This code is from the lesser size
public void pickcamera(){
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 101);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 101){
Bitmap bitmap = (Bitmap) data.getExtras().get("data");
mPreview4PsId.setImageBitmap(bitmap);
}
}
I think the problem is on the pickCamera() method which they different but when I follow the code from the lesser size the crop will not be available anymore, I stuck with this module anyone can help me? this is really help me a lot
So I just add a bunch of code which set and replace the default resolution of the Image pixel because I guess the default Pixel of the Image resolution from camera is 3000x4000 pixel so you just need to set the width and height of the ImageView. So I just set 187x250
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(),myUri);
mPreviewCashCard.setImageBitmap(Bitmap.createScaledBitmap(bitmap, 187, 250, false));
Project: Open Gallery From a Button in One Activity, Select The Images from there and Display it on the initial Activity screen.
Solution: I know that the solution for the problem above is given but it only deals with the multiple selection of images part and getting the data of the images.
My Problem:
From here, i am unable to use the uri data to convert to bitmap and hence store in my image view. I even tried
ImageView imageView = findViewById(R.id.imageView);
Bitmap bitmap = BitmapFactory.decodeFile(imageUri.getPath()) ;
imageView.setImageBitmap(bitmap); but the image doesn't show
How to get dynamic number of image views?
I want the image views on my activity.xml to be dynamic,i.e it changes its number depending on the number of selections
for eg: if the user selects 5 images on opening the gallery app, then i want to be able to display the 4 images on the initial Activity screen. If 6 images selected then 6, if 2 then 2. It should change the image view count depending on the number of selections as I dont want to hardcode the number of image views in the xml file.
How i am trying to get the bitmap is by
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
I am getting Unhandled exceptions error , java.io.FileNotFoundException for the getBitmap part below
I have attached my code below
public void onClick(View view){
Intent intent = new Intent();
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Picture"), PICK_IMAGE_MULTIPLE);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == PICK_IMAGE_MULTIPLE) {
if(resultCode == Activity.RESULT_OK) {
if(data.getClipData() != null) {
int count = data.getClipData().getItemCount(); //evaluate the count before the for loop --- otherwise, the count is evaluated every loop.
for (int i = 0; i < count; i++){
Uri imageUri = data.getClipData().getItemAt(i).getUri();
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(),imageUri) ; //MAIN ERROR OVER HERE
//OR
ImageView imageView = findViewById(R.id.imageView);
Bitmap bitmap = BitmapFactory.decodeFile(imageUri.getPath()) ;
imageView.setImageBitmap(bitmap); //STILL NOT WORKING
}
}
} else if(data.getData() != null) {
String imagePath = data.getData().getPath();
ImageView imageView = findViewById(R.id.imageView); //WANT TO AVOID THIS AND MAKE IMAGE VIEW NUMBER AS DYNAMIC
Bitmap bmImg = BitmapFactory.decodeFile(imagePath);
imageView.setImageBitmap(bmImg);
}
}
}
InputStream iStream = getContentResolver().openInputStream(selectedImageUri);
byte[] inputData = Utils.getBytes(iStream);
dbHelper.insertImage(inputData);
http://www.coderzheaven.com/2012/12/23/store-image-android-sqlite-retrieve-it/
I am trying to take a picture with the camera of "Nexus 5X API 26" and show it in ImageView field, before uploading it to Firebase.
First problem is that after taking a picture, it does not show up in imageView. I am doing the following:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(intent, CAMERA_REQUEST_CODE);}
And then I was trying to show the picture in the same way I do for the pictures taken from Gallery:
filePath = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
imageView.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
The part which I do not understand is how data.getData() works in both cases (for Gallery and for Camera)?
I guess that the uploadImage() method, should be the same for both Gallery and Camera uploads (it works for Gallery already so...).
So the thing that I am currently missing is that I am not getting the filePath, I guess?
Is it necessary to "temporarily save" the camera's picture in order to .getData()? Or it can work without any kind of "saving"?
I just want the user to take a picture and it should be sent to Firebase. Not necessary for user to see it in imageView first, just to get the uri (data) in order to upload it.
view when i open the camera
view after taking a picture
This will help you.
Intent intent= new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent,7);
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
bitmap= (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream baos=new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] b = baos.toByteArray();
imageEncoded = Base64.encodeToString(b,Base64.DEFAULT);
byte[] imageAsBytes = Base64.decode(imageEncoded.getBytes(), Base64.DEFAULT);
InputStream is=new ByteArrayInputStream(imageAsBytes);
bitmap1=BitmapFactory.decodeStream(is);
img.setImageBitmap(bitmap1);
}
I have a button in my code to take a picture:
<Button
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#drawable/cameralogo"
android:id="#+id/buttonCamera" />
When i click it it opens the camera and saves a picture, path is String mCurrentPhotoPath;
after the camera intent was displayed i want the button to show the image as background (android:background="mCurrent.....")???
how do do this?
Here is the solution.
You cannot set background only by path or URI, you'll need to create a Bitmap( and use ImageButton) or a Drawable out of it.
Using Bitmap and ImageButton:
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
yourImageButton.setImageBitmap(bitmap);
Using Drawable and Button:
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
Drawable d = new BitmapDrawable(getResources(),bitmap);
yourButton.setBackground(d);
Have you had a look at this question yet?
How to set the button background image through code
You cant do this in the xml but only programmatically. Just get a reference to the newly created picture like described here:
How to get path of a captured image in android
To start the camera intent:
...
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
activity.startActivityForResult(takePictureIntent, PHOTO_ACTIVITY_REQUEST_CODE);
...
Where PHOTO_ACTIVITY_REQUEST_CODE is just a integer constant unique within activity to be used as request codes while starting intent for results.
To Receive photo in the onActivityResult, and update background of the view
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PHOTO_ACTIVITY_REQUEST_CODE && data != null) {
Bundle extras = data.getExtras();
if (extras != null) {
Bitmap photo = (Bitmap) extras.get("data");
if (photo != null) {
// mView should refer to view whose reference is obtained in onCreate() using findViewById(), and whose background you want to update
mView.setBackground(new BitmapDrawable(getResources(), photo));
}
}
}
The above code does not use full size photo. For that, you will have to ask Photo intent to save it to a file, and read the file. Details are presenthere
UPDATE!!
So Lokesh put me on the right path, and showed me that the problem was the size of the file being too large to show in the imageview. I was able to fix the imageview preview problem with the following code under my onActivityResult:
try {
Bitmap picture = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory().getPath()+"/td01.png");
int nh = (int) ( picture.getHeight() * (512.0 / picture.getWidth()) );
Bitmap scaled = Bitmap.createScaledBitmap(picture, 512, nh, true);
Log.v("Path", Environment.getExternalStorageDirectory().getPath()+"/td01.png");
pic1.setImageBitmap(scaled);
} catch (Exception e) {
Log.e("Error reading file", e.toString());
}
Thanks Lokesh!
----------------- ORIGINAL ISSUE BELOW THIS LINE -------------------
So I'm trying to both save an image to the SD card for use later, AND display the saved image in an imageview which also serves as the button which takes the photo. Here's the code:
pic1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent camera_intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File image1 = new File(Environment.getExternalStorageDirectory(),"td01.png");
camera_intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(image1));
startActivityForResult(camera_intent, CAMERA_PIC_REQUEST1);
}
});
followed by:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode){
case 1:
if(resultCode==RESULT_OK){
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
pic1.setImageBitmap(thumbnail);
}
}
Now, if I remove the following code from the onclick, it shows the thumbnail as I expect it to:
File image1 = new File(Environment.getExternalStorageDirectory(),"td01.png");
camera_intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(image1));
...but without that code, it doesn't save the file to my sd card.
The problem is, if I don't remove that code, it saves the image to my SD Card but immediately crashes before returning to the activity after tapping SAVE in the camera activity, unless I remove the following code from my onActivityResult:
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
pic1.setImageBitmap(thumbnail);
I've also tried many variations of the following code in my onActivityResult, hoping to display it from the actual file in a different way, but it never works and only shows a blank imageview, but at least in that case the crash doesn't occur, because I removed the get extras get data code:
Bitmap photo1 = BitmapFactory.decodeFile("/sdcard/td01.png");
pic1.setImageBitmap(photo1);
I've been struggling with this for days and am at a loss here. Hope someone can show me what stupid thing I'm doing wrong and explain why this isn't working.
Thanks!
Try this:
try {
Bitmap picture = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory().getPath()+"/DCIM/MyPhoto.jpg");
Log.v("Path", Environment.getExternalStorageDirectory().getPath()+"/DCIM/MyPhoto.jpg");
mImageView.setImageBitmap(picture);
} catch (Exception e) {
Log.e("Error reading file", e.toString());
}
do NOT access the SD card directly, try accessing it trough Environment Like this
String imageDir = Environment.getExternalStorageDirectory()+"/apple.jpg";
and then you can call BitmapFactory:
Bitmap myBitmap = BitmapFactory.decodeFile(imageDir);