Read/Write Image from local File - android

How do I read and write image to/from a local file in android?
I want to take an image from a file, save it in memory and then read it from file.
I am using this code to write image in file:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
String fileName=GetFileName().replace('.', ' ')+".png";
File out = new File(Environment.getExternalStorageDirectory()+File.separator +fileName);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(out));
startActivityForResult(intent, REQUEST_IMAGE);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
wb.loadUrl(sendUrl);
if(requestCode == REQUEST_IMAGE && resultCode == Activity.RESULT_OK) {
String imgPath= data.getExtras().get("Uri").toString();
bm = BitmapFactory .decodeFile(imgPath);
}
}
How can I read the image back?

Try this---
Uri mUriSavedImage;
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
String fileName=GetFileName().replace('.', ' ')+".png";
File out = new File(Environment.getExternalStorageDirectory()+File.separator +fileName);
mUriSavedImage= Uri.fromFile(out);
intent.putExtra(MediaStore.EXTRA_OUTPUT, mUriSavedImage);
startActivityForResult(intent, REQUEST_IMAGE);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
wb.loadUrl(sendUrl);
if(requestCode == REQUEST_IMAGE && resultCode == Activity.RESULT_OK) {
String imgPath= mUriSavedImage.getPath();
bm = BitmapFactory.decodeFile(imgPath);
}
}

Related

How to set Image Button to some image in my internal storage (Android)?

Help me to set the uri of the image button to the image that i select form the internal storage. I have already given required permissions in the android_manifest.xml file. The problem is that when i browse to the internal storage of phone the images are unselectable.
public void onUploadImageClick(View view){
Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
galleryIntent.setType("Image/*");
startActivityForResult(galleryIntent, GALLERY_REQUEST);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == GALLERY_REQUEST && resultCode == RESULT_OK){
uri=data.getData();
imageButton = (ImageButton)findViewById(R.id.image_button);
imageButton.setImageURI(uri);
}
}
public void onUploadImageClick(View view){
Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, GALLERY_REQUEST);
}
Then in your on activity result method keep this
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == GALLERY_REQUEST && resultCode == RESULT_OK){
uri=data.getData();
imageButton = (ImageButton)findViewById(R.id.image_button);
imageButton.setImageURI(uri);
}
}
EDIT
yes the problem is I forgot to add .Media
so use this line of code:
Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
You can try below code
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == GALLERY_REQUEST && resultCode == RESULT_OK){
uri=data.getData();
String pathName = uri.toString();
Bitmap bmp = BitmapFactory.decodeFile(pathName);
imageButton = (ImageButton)findViewById(R.id.image_button);
imageButton.setImageBitmap(bmp);
}
}
Can you try this. You could replace ImageView to ImageButton
String qrPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Image.png";
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap mustOpen = BitmapFactory.decodeFile(qrPath, options);
setContentView(R.layout.qr_image);
ImageView imageView = (ImageView) findViewById(R.id.qr_image);
imageView.setImageBitmap(mustOpen);

Camera Shortcut onClick

Simple question. Is it possible to allow a button (preferably a FAB) to open a camera app, sort of as a short cut? This shortcut can either lead to the default camera, or lead to any downloaded camera apps.
Yes, it's possible.
You can create an implicit intent with action ACTION_IMAGE_CAPTURE.
eg:
File file = new File(Environment.getExternalStorageDirectory() + "/DCIM/", "image" + new Date().getTime() + ".png");
Uri imgUri = Uri.fromFile(file);
String imgPath = file.getAbsolutePath();
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, setImageUri());
startActivityForResult(intent, CAPTURE_IMAGE);
To handle the result:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != Activity.RESULT_CANCELED) {
if (requestCode == CAPTURE_IMAGE) {
Bitmap resultAsBitmap = BitmapFactory.decodeFile(imgPath);
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
}
PS: Don't forget to add your permissions in manifest file.

Bad image quality after picking an image from gallery

I use this to let the user pick an image from gallery:
Intent i = new Intent(
Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
i.setType("image/*");
i.putExtra("crop", "true");
i.putExtra("scale", true);
i.putExtra("return-data", true);
startActivityForResult(i, RESULT_LOAD_IMAGE);
Then it will be displayed in an ImageView:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
final Bundle extras = data.getExtras();
Bitmap photo = extras.getParcelable("data");
ImageView imageView = (ImageView) findViewById(R.id.image);
imageView.setImageBitmap(photo);
}
}
It works but the picture now has a bad quality.
How to fix this?
You shouldn't specify any of those extras because they are device-dependent. Just do this:
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, RESULT_LOAD_IMAGE);
and
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK)
{
Uri selectedImage = data.getData();
//load Uri into ImageView
}
}
I would look into using a library like Picasso to load the images instead of just doing imageView.setImageBitmap(photo) if your app is image intensive.

Save Capture Image in SD card android

I call camera intent and try to save capture image in sd card.but camera intent give me null data.what is the problem i can't understood.my code is as follow
imgBtnPicture.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String fileName="ashraful.jpg";
String root = Environment.getExternalStorageDirectory().toString();
new File(root + "/photofolder").mkdirs();
File outputfile=new File(root+"/photofolder/", fileName);
Uri outputFileUri = Uri.fromFile(outputfile);
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
});
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
if(data!=null)
{
Bitmap photo = (Bitmap) data.getExtras().get("data");
Bitmap icon1=ResizeBitmap(photo,200,200);
imgBtnPicture.setImageBitmap(icon1);
}
}
}
but i get null data? how to solve my problem?plz help
private void takeNewPicture() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
ContentValues values = new ContentValues(3);
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
cameraImagePath = getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, cameraImagePath);
startActivityForResult(takePictureIntent, CAMERA_REQUEST);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Uri imageFilePathUri = null;
if (cameraImagePath != null) {
Cursor imageCursor = getContentResolver().query(
cameraImagePath, filePathColumn, null, null, null);
if (imageCursor != null && imageCursor.moveToFirst()) {
int columnIndex = imageCursor.getColumnIndex(filePathColumn[0]);
String filePath = imageCursor.getString(columnIndex);
imageCursor.close();
imageFilePathUri = filePath != null ? Uri
.parse(filePath) : null;
}
}
}
}
cameraImagePath is a Uri object
Read this Android Training post carefully to figure things out. As the post says,
The Android Camera application saves a full-size photo if you give it a file to save into.
so if you did pass the Uri of a file, you should access the image using this Uri.

Using android camera intent, getting uri, and then using uri

I have a folowing code:
public void take_picture(View view)
{
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
ImageView slikaa = (ImageView)this.findViewById(R.id.slikaa);
if ((requestCode == CAMERA_REQUEST)&& (resultCode == Activity.RESULT_OK)) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
slikaa.setImageBitmap(photo);
}
Now my question is how to get that image path(for saving it to my database), and then again, use it to show in a picture(I don't know how to get String paths, and then re-use it)
For getting Image Path in onActivityResult you will need to Start camera by send Image Path with Intent as:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//ContentValues values = new ContentValues();
ContentValues values = new ContentValues(3);
values.put(MediaStore.Images.Media.DISPLAY_NAME, "testing");
values.put(MediaStore.Images.Media.DESCRIPTION, "this is description");
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
imageFilePath = MainActivity.this.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageFilePath);
startActivityForResult(intent, CAMERA_REQUEST);
and on onActivityResult
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
ImageView slikaa = (ImageView)this.findViewById(R.id.slikaa);
if ((requestCode == CAMERA_REQUEST)&& (resultCode == Activity.RESULT_OK)) {
//get image from path
Bitmap photo = (Bitmap) data.getExtras().get("data");
photo = BitmapFactory.decodeStream(this.getContentResolver()
.openInputStream(imageFilePath), null, op);
slikaa.setImageBitmap(pic);
//slikaa.setImageBitmap(photo);
}
String path;
Public void take_picture(){
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
File output = new File(dir,"gtumca.png");
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(output));
path = output.getAbsolutePath(); <-------------
startActivityForResult(cameraIntent, TAKE_PHOTO);
}

Categories

Resources