Save/Read Bitmap in internal storage doesn't work - android

I tried to code a simple functionallity that would crop an image (stored in the drawable folder) after pressing a button, and return the cropped image in the first Activity in an imageView.
Aftrer trying to pass the bitmap in a arraybyte through an intent that didn't work, I am now trying to save the cropped image into the internal storage and read it in the OnActivityResult function, however when I am done cropping I get nothing in my ImageView.
For the cropping I used the following library : https://github.com/ArthurHub/Android-Image-Cropper/wiki
Here is the code:
public class MainActivity extends AppCompatActivity {
Button b;
ImageView preview;
final int REQUEST_CODE_TEST = 63;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b = (Button) findViewById(R.id.chooser);
preview = (ImageView) findViewById(R.id.preview);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), Cropper.class);
startActivityForResult(intent, REQUEST_CODE_TEST);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE_TEST && resultCode == RESULT_OK) {
try {
String path=data.getData().toString();
File f=new File(path,"profile.jpg");
Bitmap bitmap = BitmapFactory.decodeStream(new FileInputStream(f));
preview.setImageBitmap(bitmap);
}catch (Exception e){
e.printStackTrace();
}
}
}
The crop actvity :
public class Cropper extends Activity {
CropImageView cropImageView;
Bitmap bitmap;
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cropper);
bitmap= BitmapFactory.decodeResource(getResources(),R.drawable.ic_bonnasse);
cropImageView=(CropImageView)findViewById(R.id.cropImageView);
cropImageView.setCropShape(CropImageView.CropShape.RECTANGLE);
cropImageView.setImageBitmap(bitmap);
button=(Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
}
#Override
public void finish() {
super.finish();
Intent retour=new Intent();
Bitmap crop=cropImageView.getCroppedImage();
Bitmap out=Bitmap.createScaledBitmap(crop, 200, 200, true);
ContextWrapper cw = new ContextWrapper(getApplicationContext());
File directory = cw.getDir("Profile_pic", Context.MODE_PRIVATE);
File mypath=new File(directory,"profile.jpg");
FileOutputStream outputStream=null;
try{
outputStream=new FileOutputStream(mypath);
out.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
outputStream.close();
}catch (Exception e){
e.printStackTrace();
}
retour.putExtra("directory",directory.getAbsolutePath());
}
Thanks for your help.

when you are finishing your crop activity , you are suppose to set your result status for your parent activity , otherwise the parent activity can not detect the result code .
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE_TEST && resultCode == RESULT_OK) {
Bitmap bitmap = (Bitmap) data.getParcelableExtra("bitmap");
preview.setImageBitmap(bitmap);
}
In this figure the result code is always RESULT_CANCEL , therefore your If block does not run.
Solution :
Do not override finish() method , just use it and change your listener with this in crop activity .
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent retour=new Intent();
Bitmap crop = BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher);
Bitmap out=Bitmap.createScaledBitmap(crop, 200, 200, true);
retour.putExtra("bitmap", out);
setResult(RESULT_OK, retour);
finish();
}
});
In this case I just passed the bitmap into main activity , but if you are willing to save the bitmap then send it to main activity , you are suppose to save it and put it into retour.putExtra("directory",directory.getAbsolutePath());
in your crop activity , then get it by String directory = data.getStringExtra("directory"); in onActivityResult .
Although in my view the first way is better , you have a second one.

Related

I need to capture Image and display it in another activity

I have an activity that starts the camera API.
I want to press the button (named by id "cptr_1") and take a picture and display it in another activity (PhotoPreview.class) where I can add photo effects.
I just need the code for:
ImageButton capture_1 = (ImageButton)findViewById(R.id.cptr_1);
capture_1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
And then how to transfer that image to PhotoPreview.class
You can take photo with the camera app of the device.
So when you click:
static final int ImageValue= 1;
ImageButton capture_1 = (ImageButton)findViewById(R.id.cptr_1);
capture_1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent takepic = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takepic.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takepic, ImageValue);
}
}
});
Once capture is completed get the image back from the camera application
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == ImageValue && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
}
}
Then send Bitmap to another activity.
Inside the activity that starts the camera API write:
Intent intent = new Intent(this, PhotoPreview.class);
intent.putExtra("GetBitmap", bitmap);
Inside PhotoPreview.class write:
Intent intent = getIntent();
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("GetBitmap");
Also you may need to add those permissions to Android Manifest
<uses-feature android:name="android.hardware.camera"
android:required="true" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
either you can use an Intent to take picture or use a custom camera class
for camera intent for custom camera class
if u already have a custom camera class, you can use this code`Camera cam = Camera.open(cameraId);
capture_1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
cam.takePicture(null, null, mPicture);
} catch (Exception e) {
}
}
});
private PictureCallback mPicture = new PictureCallback() {
public void onPictureTaken(final byte[] data, Camera camera) {
try {
File mediaFile = new File("file path/filename.jpg");
FileOutputStream fos = new FileOutputStream(mediaFile);
fos.write(data);
fos.close();
cam.startPreview();
} catch (Exception e) {
}
}
};`

bitmap bad quality after set to imageview

I am creating a app that opens camera for user and after image captured it will be shows on ImageView! But the image in ImageView has very bad quality
here is the code:
public class Camera extends AppCompatActivity implements View.OnClickListener {
ImageView imgView;
Button camera, setBackground;
Intent i;
int cameraData = 0;
Bitmap bitmap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.camera);
initialize();
}
private void initialize() {
imgView = (ImageView) findViewById(R.id.bPicture);
camera = (Button) findViewById(R.id.bOpenCamera);
setBackground = (Button) findViewById(R.id.bSetBackground);
camera.setOnClickListener(this);
setBackground.setOnClickListener(this);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK){
Bundle extras = data.getExtras();
bitmap = (Bitmap) extras.get("data");
Bitmap resizedBmp = Bitmap.createScaledBitmap(bitmap, imgView.getWidth(),imgView.getHeight(), false);
imgView.setImageBitmap(resizedBmp);
}
}
#Override
public void onClick(View v) {
switch(v.getId()){
case R.id.bOpenCamera:
i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, cameraData);
break;
case R.id.bSetBackground:
try {
WallpaperManager wallmngr = WallpaperManager.getInstance(this);
wallmngr.setBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}
}
what should I do to increase image quality?
Use something other than the thumbnail. Quoting the documentation for ACTION_IMAGE_CAPTURE, with emphasis added:
The caller may pass an extra EXTRA_OUTPUT to control where this image will be written. If the EXTRA_OUTPUT is not present, then a small sized image is returned as a Bitmap object in the extra field. This is useful for applications that only need a small image. If the EXTRA_OUTPUT is present, then the full-sized image will be written to the Uri value of EXTRA_OUTPUT.
So, specify a Uri in EXTRA_OUTPUT where a full camera image should be written to. Then, use an image-loading library, like Picasso, to load the photo into your ImageView.
Here is a sample app that demonstrates using EXTRA_OUTPUT.
This is how I made it work for me! Assuming you are capturing an image, and would like to show the captured image in a new Activity, you can follow this way:
First on the click of button you can:
public void cameraFuture(View view) // <-- onClick() method of Camera Button
{
Intent intent= new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file = new File(Environment.getExternalStorageDirectory(),
"MyPhoto.jpg");
outPutfileUri = Uri.fromFile(file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outPutfileUri);
startActivityForResult(intent, TAKE_PIC);
}
Then on the onActivityResult() method, you can do this way:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == TAKE_PIC && resultCode==RESULT_OK){
Toast.makeText(this, outPutfileUri.toString(),Toast.LENGTH_LONG).show();
Intent bitIntent = new Intent(this, CameraTake.class);
bitIntent.putExtra("imageUri", outPutfileUri);
startActivity(bitIntent);
finish();
}
}
And in the next Activity, you can receive the file this way:
Intent receiveIntent = getIntent();
uri = receiveIntent.getParcelableExtra("imageUri");
if (savedInstanceState == null) {
Bundle extras = getIntent().getExtras();
if(extras == null) {
receiveImg= null;
} else {
receiveImg= extras.getString("PASSER");
}
} else {
receiveImg= (String) savedInstanceState.getSerializable("PASSER");
}
File imgFile = new File(Environment.getExternalStorageDirectory(),"MyPhoto.jpg");
if(imgFile.exists()){
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ImageView myImage = (ImageView) findViewById(R.id.camOut);
myImage.setImageBitmap(myBitmap);
}

Blurred Image Issue in Imageview

I have implemented Camera via Intent and set that Image in Imageview which is appearing blurred, while the same Image appear's clear in ImageGallery. The code and related picture are as follows :
Image saved in SD CARD
Blurred image in Imageview
Activity Class :
public class MainActivity extends ActionBarActivity {
Button b1,b2;
ImageView iv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button)findViewById(R.id.button);
iv=(ImageView)findViewById(R.id.imageView);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 0);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
Bitmap bp = (Bitmap) data.getExtras().get("data");
iv.setImageBitmap(bp);
}
File is being saved in sdcard and also I retrive its URI sucessfully, but still imageview do not show this image or imageview disappear when camera click and return. Code are as follows -:
public class MainActivity extends Activity {
static int TAKE_PIC =1;
Uri outPutfileUri;
ImageView mImageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mImageView = (ImageView) findViewById(R.id.image);
}
public void CameraClick(View v) {
Intent intent= new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file = new File(Environment.getExternalStorageDirectory(),
"MyPhoto.jpg");
outPutfileUri = Uri.fromFile(file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outPutfileUri);
startActivityForResult(intent, TAKE_PIC);
}
Bitmap bitmap = null;
#Override
protected void onActivityResult(int requestCode, int resultCode,Intent data)
{
if (requestCode == TAKE_PIC && resultCode==RESULT_OK) {
String uri = outPutfileUri.toString();
Log.e("uri-:", uri);
mImageView.setImageURI(Uri.parse(uri));
}
}}
Please suggest me how to get path of clicked image and set it to imageview. I am new here.
First of all, the image is blurred because the intent data contains only the thumbnail of the image.
you must save the image on the internal/external storage and then acquire it.
feel free to take a look at the android developers documentation regarding camera images:
Android Developers
and this older SO answer explains how to retrieve the full size image after saving it :
Android get full size image from camera
Try This,
public class MainActivity extends Activity {
static int TAKE_PIC =1;
Uri outPutfileUri;
ImageView mImageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mImageView = (ImageView) findViewById(R.id.image);
}
public void CameraClick(View v) {
Intent intent= new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file = new File(Environment.getExternalStorageDirectory(),
"MyPhoto.jpg");
outPutfileUri = Uri.fromFile(file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outPutfileUri);
startActivityForResult(intent, TAKE_PIC);
}
Bitmap bitmap = null;
#Override
protected void onActivityResult(int requestCode, int resultCode,Intent data)
{
if (requestCode == TAKE_PIC && resultCode==RESULT_OK) {
String uri = outPutfileUri.toString();
Log.e("uri-:", uri);
Toast.makeText(this, outPutfileUri.toString(),Toast.LENGTH_LONG).show();
//Bitmap myBitmap = BitmapFactory.decodeFile(uri);
// mImageView.setImageURI(Uri.parse(uri)); OR drawable make image strechable so try bleow also
try {
bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), outPutfileUri);
Drawable d = new BitmapDrawable(getResources(), bitmap);
mImageView.setImageDrawable(d);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Hope this will help you enjoy!!!!!

save picture and open in imageview

When I take a picture, the picture will saved on the hard drive and open the picture in imageview.
In my command the picture will be saved but the app is crashing.
public class Note extends Activity {
TextView t;
ImageView iv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.note);
iv=(ImageView) findViewById(R.id.imageView);
Button btn = (Button) findViewById(R.id.photo);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 0);
}
});
}
public void button (View view){
Intent intent = new Intent(view.getContext(),Note2.class);
startActivityForResult(intent, 0);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode==0)
{
Bitmap theImage = (Bitmap) data.getExtras().get("data");
iv.setImageBitmap(theImage);
}}
}
How do I rewrite the command to make it work?
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode==0)
{
Bitmap theImage = (Bitmap) data.getExtras().get("data");
iv.setImageBitmap(theImage);
First, make sure that your Manifest has this permissions:
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Second, your onActivityResult method should be like this with declaring a File and store the Bitmap into the device as following:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
switch(requestCode){
case 0:
final File file = getTempFile(this);
try {
Bitmap captureBmp = Media.getBitmap(getContentResolver(), Uri.fromFile(file) );
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}
}
Here is a very good example on capturing images

How can I store the imagePath of the image that I took using the camera activity into the database and retrieving the path?

how to store the imagePath of the image that I had captured using the camera activity to database. I also need to retrieve that imagePath and display on another activity?
Can someone help me please?
Update:
public class Image_secondPage extends Activity
{
public static final int CAMERA_RESULT = 1;
Button btn1;
ImageView imageView1;
private final String tag = getClass().getName();
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.image_secondpage);
imageView1 = (ImageView)findViewById(R.id.imageView1);
Button btnNext = (Button)findViewById(R.id.buttonNext);
btnNext.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Intent intent = new Intent(Image_secondPage.this, ImagesPage.class);
startActivity(intent);
}
});
PackageManager pm = getPackageManager();
if(pm.hasSystemFeature(PackageManager.FEATURE_CAMERA))
{
Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra(MediaStore.EXTRA_OUTPUT, MyFileContentProvider.CONTENT_URI);
startActivityForResult(i, CAMERA_RESULT);
}
else
{
Toast.makeText(getBaseContext(), "Camera is not available", Toast.LENGTH_LONG).show();
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
Log.i(tag, "Receive the camera result");
if(resultCode == RESULT_OK && requestCode == CAMERA_RESULT)
{
File out = new File(getFilesDir(), "newImage.jpg");
if(!out.exists())
{
Toast.makeText(getBaseContext(), "Error while capturing image", Toast.LENGTH_LONG).show();
return;
}
Bitmap mBitmap = BitmapFactory.decodeFile(out.getAbsolutePath());
imageView1.setImageBitmap(mBitmap);
}
}
#Override
protected void onDestroy()
{
super.onDestroy();
imageView1 = null;
}
I am supposed to get the imagePath that I have captured and show the image on the imageView that I had on the next class but I don't know how. Can someone help me?
try use this
bitmap = (Bitmap) data.getExtras().get("data");
this will give you bitmap image, then you can save it to database or anything you want.
let me know if this not solve your problem.

Categories

Resources