Android Saving Captured Image As Android Camera Captured
By : RichG
Date : March 29 2020, 07:55 AM
wish helps you Your stack trace seems self-explantory: you are accessing a closed Cursor from indexingForImageMediaStore().
|
Imageview not displaying image from Camera in Activity
By : user4183
Date : March 29 2020, 07:55 AM
I wish this helpful for you I think you are doing too many things at the same time and not knowing what fails. First try to display the image in the first activity. Once you know how to display the image correctly, you can pass the file path to the second activity. I tried to use your code but it didn't even go the the second activity because mFileUri is always null. Also in your onActivityResult method you should filter the request code not only the resultcode. I edited your code, remove unused stuff and it works. code :
public class MainActivity extends Activity {
private String mFileUri;
private final Context mContext = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnbutton1 = (Button) findViewById(R.id.button1);
btnbutton1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Uri uri = createPictureFile();
mFileUri = uri.getEncodedPath();
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(intent, 100);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 100 && resultCode == RESULT_OK) {
if (mFileUri != null) {
Intent intent = new Intent(mContext, SecondActivity.class);
intent.putExtra("filepath", mFileUri);
startActivity(intent);
}
}
}
private static File getOutputMediaFile(int type) {
File mediaStorageDir = new File(Environment.getExternalStorageDirectory(), "DCIM/Camera");
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
return null;
}
}
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
File mediaFile;
if (type == 1) { // image
mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");
} else if (type == 2) { // video
mediaFile = new File(mediaStorageDir.getPath() + File.separator + "VID_" + timeStamp + ".mp4");
} else {
return null;
}
return mediaFile;
}
public Uri createPictureFile() {
String storageState = Environment.getExternalStorageState();
if (storageState.equals(Environment.MEDIA_MOUNTED)) {
File pictureDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
pictureDir = new File(pictureDir, "MyApp");
// Create the storage directory if it does not exist
if (!pictureDir.exists()) {
if (!pictureDir.mkdirs()) {
Log.d("user", "failed to create directory");
return null;
}
}
//Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String fileName = pictureDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg";
File imageFile = new File(fileName);
// Convert to URI and return
return Uri.fromFile(imageFile);
} else {
Log.d("user", "No media mounted");
return null;
}
}
}
public class SecondActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent intent = getIntent();
String filepath = intent.getStringExtra("filepath");
ImageView imageView = (ImageView) findViewById(R.id.imageView1);
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inSampleSize = 6;
Bitmap bm = BitmapFactory.decodeFile(filepath, opts);
imageView.setImageBitmap(bm);
}
}
public class ExifUtil {
/**
* @see http://sylvana.net/jpegcrop/exif_orientation.html
*/
public static Bitmap rotateBitmap(String src, Bitmap bitmap) {
try {
int orientation = getExifOrientation(src);
if (orientation == 1) {
return bitmap;
}
Matrix matrix = new Matrix();
switch (orientation) {
case 2:
matrix.setScale(-1, 1);
break;
case 3:
matrix.setRotate(180);
break;
case 4:
matrix.setRotate(180);
matrix.postScale(-1, 1);
break;
case 5:
matrix.setRotate(90);
matrix.postScale(-1, 1);
break;
case 6:
matrix.setRotate(90);
break;
case 7:
matrix.setRotate(-90);
matrix.postScale(-1, 1);
break;
case 8:
matrix.setRotate(-90);
break;
default:
return bitmap;
}
try {
Bitmap oriented = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
bitmap.recycle();
return oriented;
} catch (OutOfMemoryError e) {
e.printStackTrace();
return bitmap;
}
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}
private static int getExifOrientation(String src) throws IOException {
int orientation = 1;
try {
/**
* if your are targeting only api level >= 5
* ExifInterface exif = new ExifInterface(src);
* orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
*/
if (Build.VERSION.SDK_INT >= 5) {
Class<?> exifClass = Class.forName("android.media.ExifInterface");
Constructor<?> exifConstructor = exifClass.getConstructor(new Class[] { String.class });
Object exifInstance = exifConstructor.newInstance(new Object[] { src });
Method getAttributeInt = exifClass.getMethod("getAttributeInt", new Class[] { String.class, int.class });
Field tagOrientationField = exifClass.getField("TAG_ORIENTATION");
String tagOrientation = (String) tagOrientationField.get(null);
orientation = (Integer) getAttributeInt.invoke(exifInstance, new Object[] { tagOrientation, 1});
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
return orientation;
}
|
Save the image from an ImageView(captured from camera) in my custom defined folder in the INTERNAL MEMORY
By : Hüseyin Çakanlı
Date : March 29 2020, 07:55 AM
wish of those help I have been referring various posts and sites and I have been trying this since 3 days and I cannot figure this out! , Got it working: Code for saving: code :
FileOutputStream outStream = null;
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File(sdCard.getAbsolutePath() + "/camtest");
dir.mkdirs();
String fileName = String.format("%d.jpg", System.currentTimeMillis());
File outFile = new File(dir, fileName);
outStream = new FileOutputStream(outFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
outStream.flush();
outStream.close();
refreshGallery(outFile);
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
|
ImageView is not showing captured Image using Camera in android
By : SurfBoy Steve
Date : March 29 2020, 07:55 AM
this will help I am trying to choose an image for registration. When clicking the ImageView, the user will be given a choice between taking a picture or choosing from his/her gallery. When the user chooses the gallery option, it will display the selected image. When the user chooses the camera option, the ImageView does not display the image. Below is my code for the OnActivityResult , I think it should be like this code :
Bitmap imageBitmap = (Bitmap) extras.get("data");
mImageView.setImageBitmap(imageBitmap);
|
android selected camera image through gallery intent is not displaying in imageview
By : Amith Mandula
Date : October 08 2020, 03:00 AM
hop of those help? Bug #1: ACTION_PICK does not use a MIME type. Either use ACTION_GET_CONTENT with a MIME type, or use ACTION_PICK with a collection Uri (e.g., MediaStore.Images.Media.EXTERNAL_CONTENT_URI). Bug #2: You are assuming that MediaStore knows how to get an image from that Uri. Since the Uri may not have come from the MediaStore, that is an incorrect assumption. Use Glide or Picasso to load the image into your ImageView.
|