Saving bitmap at custom path and with custom name - android

my program captures an image (as bitmap) but I don't know (and couldn't find) how to save it with a custom name at a custom path... Here's my image capture codes. Can I do it? Or is there any way saving it as another data type, not bitmap? some help will be great. Thanks.
public class ShowMessagesPage extends Activity {
final static int CAMERA_RESULT = 0;
DBAdapter db = new DBAdapter(this);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_messages_page);
Button userButton = (Button)findViewById(R.id.button1);
userButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
error();
}});
Button buttonPhoto = (Button)findViewById(R.id.buttonPhoto);
buttonPhoto.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, CAMERA_RESULT);
}});}
protected void onActivityResult(int requestCode, int resultCode,
Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (resultCode == RESULT_OK) {
Bundle extras = intent.getExtras();
Bitmap bmp = (Bitmap) extras.get("data");
}
}
and the other codes..........

Try this:
Intent getCameraImage = new Intent("android.media.action.IMAGE_CAPTURE");
File cameraFolder;
if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
cameraFolder = new File(android.os.Environment.getExternalStorageDirectory(),"folder_name_of_your_choice");
else
cameraFolder= ShowMessagesPage.this.getCacheDir();
if(!cameraFolder.exists())
cameraFolder.mkdirs();
File photo = new File(Environment.getExternalStorageDirectory(), "folder_name_of_your_choice/camera_snap.jpg");
getCameraImage.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
initialURI = Uri.fromFile(photo);
startActivityForResult(getCameraImage, CAMERA_RESULT);
And in the onActivityResult(), process the Uri initialURI to get your Image which is already saved to the path you have chosen in the code posted above. The name of the image file will be: camera_snap.jpg
Note: The Uri initialURI is declared globally.

Related

android bitmap does not get maximum resolution [duplicate]

I am making an app that sends a picture taken from the Camera app however the image it returns seems to be only a thumbnail how can I get it to turn the whole image?
The following code gets an image, but it's too small.
public class OnTheJobActivity extends Activity{
private static final int CAMERA_PIC_REQUEST = 1337;
private Button takePictureButton;
private Button sendPictureButton;
private Bitmap thumbnail;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setRequestedOrientation(
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.onthejob);
takePictureButton = (Button) findViewById(R.id.takePictureButton);
takePictureButton.setOnClickListener(takePictureButtonListener);
sendPictureButton = (Button) findViewById(R.id.sendPictureButton);
sendPictureButton.setOnClickListener(sendPictureButtonListener);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_PIC_REQUEST) {
thumbnail = (Bitmap) data.getExtras().get("data");
ImageView image = (ImageView) findViewById(R.id.photoResultView);
image.setImageBitmap(thumbnail);
sendPictureButton.setVisibility(Button.VISIBLE);
}
}
private OnClickListener takePictureButtonListener = new OnClickListener() {
#Override
public void onClick(View arg0){
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
}
};
private OnClickListener sendPictureButtonListener = new OnClickListener() {
#Override
public void onClick(View arg0){
Intent i = new Intent(Intent.ACTION_SEND);
i.putExtra(Intent.EXTRA_EMAIL, "abc#gmail.com");
i.putExtra(Intent.EXTRA_SUBJECT,"On The Job");
i.putExtra(Intent.EXTRA_STREAM, thumbnail);
i.setType("image/bmp");
startActivity(Intent.createChooser(i,"Emailfile"));
}
};
}
You could also change the intent you're using.
//in your buttonListener
ContentValues values = new ContentValues();
imageUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
//create new Intent
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
try{
startActivityForResult(i, ACTIVITY_GET_IMAGE);
}
catch(Exception ex){
Log.v("BRE", ex.toString());
}
//in your activity
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == ACTIVITY_GET_IMAGE){
if(resultCode == RESULT_OK){
try{String uri = data.getData().toString()}
catch(NullPointerException e){//do something}
}
}
}
This will return a uri which you can then use to access the full resolution image
Try using the implementation shown here
Specifically:
Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
public void onPictureTaken(byte[] imageData, Camera c) {
}
};

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!!!!!

Draw bitmap in different activity

So my app should capture image with intent then draw that image in different activity, but it doesn't work. Here is my code:
Main activity:
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File photo = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "pic.jpg");
imageUri = Uri.fromFile(photo);
startActivityForResult(intent, 2);
}
});
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 2) {
Intent k = new Intent(MainActivity.this, FullActivity.class);
startActivity(k);
}
}
Second activity:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Uri selectedImage = imageUri;
iv.setImageBitmap(bp);
}
Can someone help me and show where the problem is. Thanks in advance.
You need to pass the Uri as a string with the Intent in your MainActivity.
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 2) {
Intent k = new Intent(MainActivity.this, FullActivity.class);
k.putExtra("uri", imageUri.toString());
startActivity(k);
}
}
Then get the string in your FullActivity and parse it to a Uri, then use that to set your ImageView.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
String uriString = getIntent().getStringExtra("uri");
Uri selectedImage = Uri.parse(uriString);
// you may also need to call imageView.setImageURI(null); here
// or get the bitmap first to use imageView.setImageBitmap();
imageView.setImageURI(selectedImage);
}
From here
Called when an activity you launched exits, giving you the requestCode
you started it with, the resultCode it returned, and any additional
data from it
Your onActivityResult never get called when starting Second activity
Try send the uri you receive in Main activity in Bundle
Uri imageUri = intent.getData();
Intent intent = new Intent(MainActivity.this, SecondActiviy.class);
intent.putExtra("mUri", imageUri.toString());
And you can get this uri on SecondActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String uri= getIntent().getStringExtra("mUri");
Uri imageUri= Uri.parse(uri);
Bitmap bitmap=MediaStore.Images.Media.getBitmap(this.getContentResolver(),imageUri);
}
// Global Declaration<br>
private Uri fileUri;
private static final int REQUEST_CODE_PHOTO = 101;
// Click for capture Image
fileUri = getOutputMediaFileUri(REQUEST_CODE_PHOTO); // create a file to save the image
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
startActivityForResult(intent, REQUEST_CODE_PHOTO);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
try {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_PHOTO) {
if (resultCode == RESULT_OK) {
String PATH = fileUri.getPath();
Intent k = new Intent(MainActivity.this, FullActivity.class);
k.putExtra("KEY_FOR_PATH, PATH);
startActivity(k);
}
}
}
}
// In FullActivity
String pathOfFile = getIntent().getExtras().getString("KEY_FOR_PATH");
Bitmap myBitmap = BitmapFactory.decodeFile(pathOfFile);
//Now set myBitmap in ImageView
imageView.setImageBitmap(myBitmap)
// Add this method
private Uri getOutputMediaFileUri(int type){
File imagesFolder = new File(Environment.getExternalStorageDirectory(), "YOUR IMAGE FOLDER");
if(imagesFolder != null && !imagesFolder.exists()) {
imagesFolder.mkdirs();
}
int photonum = AppTypeDetails.getInstance(MainActivity.this).getImageName();
AppTypeDetails.getInstance(ReportTab.this).setImageName(++photonum);
File image = new File(imagesFolder, String.format("image%08d.jpeg", photonum));
return Uri.fromFile(image);
}

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.

When using the Camera app in Android, how can I return the whole image instead of just the thumbnail?

I am making an app that sends a picture taken from the Camera app however the image it returns seems to be only a thumbnail how can I get it to turn the whole image?
The following code gets an image, but it's too small.
public class OnTheJobActivity extends Activity{
private static final int CAMERA_PIC_REQUEST = 1337;
private Button takePictureButton;
private Button sendPictureButton;
private Bitmap thumbnail;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setRequestedOrientation(
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.onthejob);
takePictureButton = (Button) findViewById(R.id.takePictureButton);
takePictureButton.setOnClickListener(takePictureButtonListener);
sendPictureButton = (Button) findViewById(R.id.sendPictureButton);
sendPictureButton.setOnClickListener(sendPictureButtonListener);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_PIC_REQUEST) {
thumbnail = (Bitmap) data.getExtras().get("data");
ImageView image = (ImageView) findViewById(R.id.photoResultView);
image.setImageBitmap(thumbnail);
sendPictureButton.setVisibility(Button.VISIBLE);
}
}
private OnClickListener takePictureButtonListener = new OnClickListener() {
#Override
public void onClick(View arg0){
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
}
};
private OnClickListener sendPictureButtonListener = new OnClickListener() {
#Override
public void onClick(View arg0){
Intent i = new Intent(Intent.ACTION_SEND);
i.putExtra(Intent.EXTRA_EMAIL, "abc#gmail.com");
i.putExtra(Intent.EXTRA_SUBJECT,"On The Job");
i.putExtra(Intent.EXTRA_STREAM, thumbnail);
i.setType("image/bmp");
startActivity(Intent.createChooser(i,"Emailfile"));
}
};
}
You could also change the intent you're using.
//in your buttonListener
ContentValues values = new ContentValues();
imageUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
//create new Intent
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
try{
startActivityForResult(i, ACTIVITY_GET_IMAGE);
}
catch(Exception ex){
Log.v("BRE", ex.toString());
}
//in your activity
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == ACTIVITY_GET_IMAGE){
if(resultCode == RESULT_OK){
try{String uri = data.getData().toString()}
catch(NullPointerException e){//do something}
}
}
}
This will return a uri which you can then use to access the full resolution image
Try using the implementation shown here
Specifically:
Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
public void onPictureTaken(byte[] imageData, Camera c) {
}
};

Categories

Resources