I want to capture image and save it in Image view in one of My Application. I already have an idea how to implement it and also Works fine in all device Except Samsung Galaxy S3.
What I Want: Avoid the Force Closing Issue Coming in Samsung Galaxy S3 and probably for all other Device which i have not yet tested.
What I have Done: I have Done the Below Code to achieve my goal.
BuildInukshk_4 Activity :
package com.inukshk.CreateInukshk;
import java.util.Calendar;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.StatFs;
import android.provider.MediaStore;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.inukshk.R;
import com.inukshk.constant.ImageCurve;
public class BuildInukshk_4 extends Activity implements OnClickListener,
OnDismissListener {
String TAG = "BuildInukshk_4";
Button btn_back, btn_upload_picture;
TextView txt_next;
public static Activity BuildInukshk_4Activity;
// Upload Camera
public static String media = null;
public static String media_thumb = null;
public static String media_type = null;
AlertDialog dialog;
String _path, Place = "";
ImageView imageView;
boolean flag = true;
Uri uriVideo = null, mCapturedImageURI = null;
private static final int SELECT_PICTURE = 1;
private String selectedImagePath = "";
Uri selectedImageUri = null;
private String filemanagerstring;
Bitmap mBitmap;
// From Transperant
ContentResolver crThumb;
private static final int CAMERA_PIC_REQUEST = 2500, VIDEO_REQUEST_CODE = 3;
final static int REQUEST_VIDEO_CAPTURED = 2;
BitmapFactory.Options options;
String fileName;
long id, imageid;
Cursor cursor, cursorimage, thumbCursor;
private String selectedthumbpath = "";
boolean finish = false;
// ADDED12324
// private String filemanagerstring;
Bitmap curThumb = null, curThumb1 = null;
String[] thumbColumns = { MediaStore.Video.Thumbnails.DATA,
MediaStore.Video.Thumbnails.VIDEO_ID };
String[] mediaColumns = { MediaStore.Video.Media._ID,
MediaStore.Video.Media.DATA };
String[] imageColumns = { MediaStore.Images.Media._ID,
MediaStore.Images.Media.DATA };
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.buildinukshk_4);
BuildInukshk_4Activity = this;
btn_back = (Button) findViewById(R.id.btn_back);
btn_back.setOnClickListener(this);
btn_upload_picture = (Button) findViewById(R.id.btn_upload_picture);
btn_upload_picture.setOnClickListener(this);
txt_next = (TextView) findViewById(R.id.txt_next);
txt_next.setOnClickListener(this);
imageView = (ImageView) findViewById(R.id.img_upload_pic);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == txt_next) {
if (selectedImagePath.equals("")) {
Toast.makeText(BuildInukshk_4.this,
"Please Upload Picture First", 3).show();
} else {
startActivity(new Intent(BuildInukshk_4.this,
BuildInukshk_5.class));
}
// this.finish();
} else if (v == btn_back) {
// startActivity(new Intent(BuildInukshk_4.this,
// BuildInukshk_3.class));
this.finish();
} else if (v == btn_upload_picture) {
registerForContextMenu(btn_upload_picture);
openContextMenu(btn_upload_picture);
}
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
Log.i(TAG, "Inside onCreateContextMenu");
super.onCreateContextMenu(menu, v, menuInfo);
if (v == btn_upload_picture) {
menu.setHeaderTitle("Upload Media");
menu.add(0, v.getId(), 0, "Photo");
}
}
@Override
public boolean onContextItemSelected(MenuItem item) {
// TODO Auto-generated method stub
if (item.getTitle() == "Photo") {
media_type = "image";
// media_thumb = null;
/*
* Intent intent = new Intent(getApplicationContext(),
* Transperant.class); intent.putExtra("MEDIA_TYPE", media_type);
* startActivity(intent);
*/
ImageChooseOptionDialog();
// ImageChooseOptionDialog();
}
return super.onContextItemSelected(item);
}
// Transperant Class
private void ImageChooseOptionDialog() {
// finish = true;
Log.i(TAG, "Inside ImageChooseOptionDialog");
dialog = new AlertDialog.Builder(BuildInukshk_4.this).create();
dialog.setTitle("Upload Photo");
dialog.setMessage("Choose your Photo From here.");
dialog.setOnDismissListener(this);
dialog.setButton("Camera", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// dialog.dismiss();
Log.i(TAG, "Inside ImageChooseOptionDialog inside camera");
Boolean isSDPresent = android.os.Environment
.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED);
mCapturedImageURI = null;
if (isSDPresent) {
// yes SD-card is present
StatFs stat = new StatFs(Environment
.getExternalStorageDirectory().getPath());
long bytesAvailable = (long) stat.getBlockSize()
* (long) stat.getAvailableBlocks();
long megAvailable = bytesAvailable / (1024 * 1024);
Log.e("", "Available MB : " + megAvailable);
if (megAvailable > 2) {
Calendar cal = Calendar.getInstance();
_path = cal.getTimeInMillis() + ".jpg";
String fileName = _path;
// File file = new File(_path);
// mCapturedImageURI = Uri.fromFile(file);
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, fileName);
mCapturedImageURI = getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
values);
Log.d(TAG, "----- path ----- " + _path);
Intent intent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT,
mCapturedImageURI);
Log.d(TAG, "----- mCapturedImageURI ******----- "
+ mCapturedImageURI);
startActivityForResult(intent, 1212);
flag = false;
} else {
Toast.makeText(BuildInukshk_4.this,
"No Memory Available", 2).show();
flag = true;
}
} else {
// Sorry
// StatFs stat = new StatFs(Environment.getDataDirectory()
// .getPath());
// long bytesAvailable = (long) stat.getBlockSize()
// * (long) stat.getAvailableBlocks();
// long megAvailable = bytesAvailable / (1024 * 1024);
// Log.e("", "Internal MB : " + megAvailable);
// if (megAvailable > 2) {
// startActivityForResult(new Intent(Transperant.this,
// VideoRecorder.class), REQUEST_VIDEO_CAPTURED);
//
// flag = false;
// } else {
Toast.makeText(BuildInukshk_4.this, "No Memory Available",
2).show();
flag = true;
// }
// Toast.makeText(Transperant.this, "No External Storage",
// 2)
// .show();
}
}
});
dialog.setButton2("Gallery", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Log.i(TAG, "Inside Gallary");
Log.i(TAG, "Inside ImageChooseOptionDialog inside camera");
// dialog.dismiss();
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(
Intent.createChooser(intent, "Select Picture"),
SELECT_PICTURE);
flag = false;
}
});
dialog.show();
}
@Override
public void onDismiss(DialogInterface dialog) {
// TODO Auto-generated method stub
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
if (cursor != null) {
// HERE YOU WILL GET A NULLPOINTER IF CURSOR IS NULL
// THIS CAN BE, IF YOU USED OI FILE MANAGER FOR PICKING THE MEDIA
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} else
return null;
}
public void onActivityResult(int requestCode, int resultCode,
final Intent data) {
crThumb = getContentResolver();
options = new BitmapFactory.Options();
options.inSampleSize = 1;
Log.i(TAG, "Inside onActivityResult");
if (requestCode == SELECT_PICTURE) {
if (resultCode == RESULT_OK) {
Log.i(TAG, "SELECT_PICTURE");
selectedImageUri = data.getData();
// filemanagerstring = selectedImageUri.getPath();
selectedImagePath = getPath(selectedImageUri);
if (selectedImagePath != null) {
BuildInukshk_4.media = selectedImagePath;
Log.e("Image path", selectedImagePath);
mBitmap = BitmapFactory.decodeFile(media);
if (mBitmap != null) {
Log.i(TAG, "Inside Set Image");