[英]Android: how to save image in my created folder with its actual size
我想将照片以实际大小保存在创建的文件夹中。 为此,我尝试了很多代码,但所有情况下,照片都以其实际大小保存在图库中,但是当我将照片保存在自己的位置时,照片将被压缩。 之后,我尝试将照片从图库复制到我的文件夹中,以获取其实际大小的照片。 在这种情况下,照片正在处理中,但没有实际尺寸。 它再次被压缩。 打开相机:
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(
Intent.createChooser(cameraIntent, "Select Picture"),
CAMERA_REQUEST);
之后在onActivityResult方法中:
try {
if (requestCode == CAMERA_REQUEST) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
if (photo != null) {
imageView.setImageBitmap(photo);
}
// Image name
final ContentResolver cr = getContentResolver();
final String[] p1 = new String[] {
MediaStore.Images.ImageColumns._ID,
MediaStore.Images.ImageColumns.DATE_TAKEN };
Cursor c1 = cr.query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, p1, null,
null, p1[1] + " DESC");
if (c1.moveToFirst()) {
String uristringpic = "content://media/external/images/media/"
+ c1.getInt(0);
Uri newuri = Uri.parse(uristringpic);
// Log.i("TAG", "newuri "+newuri);
String snapName = getRealPathFromURI(newuri);
Uri u = Uri.parse(snapName);
File f = new File("" + u);
String fileName = f.getName();
editTextPhoto.setText(fileName);
checkSelectedItem = true;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
photo.compress(CompressFormat.PNG, 0 /* ignored for PNG */,
bos);
byte[] bitmapdata = bos.toByteArray();
// Storing Image in new folder
// StoreByteImage(mContext, bitmapdata, 100, fileName);
copyImageFromGallery(newuri);
// To appear images in created folder of Gallery dynamically
// as soon as they
// are captured.
sendBroadcast(new Intent(
Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://"
+ Environment.getExternalStorageDirectory())));
// Delete the image from the Gallery
getContentResolver().delete(newuri, null, null); // for delete
}
c1.close();
}
} catch (NullPointerException e) {
System.out.println("Error in creating Image." + e);
} catch (Exception e) {
System.out.println("Error in creating Image." + e);
}
现在,我将图像表单库复制到我的文件夹中:
protected void copyImageFromGallery(Uri uri){
try {
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if (sd.canWrite()) {
String sourceImagePath= uri.toString();
File sdImageMainDirectory = new File(
Environment.getExternalStorageDirectory() + "/pix/images");
if (!sdImageMainDirectory.exists()) {
sdImageMainDirectory.mkdirs();
}
String destinationImagePath= Environment.getExternalStorageDirectory() + "/pix/images";
File source= new File(data, souceImagePath);
File destination= new File(sd, destinationImagePath);
if (source.exists()) {
FileChannel src = new FileInputStream(source).getChannel();
FileChannel dst = new FileOutputStream(destination).getChannel();
dst.transferFrom(src, 100, src.size());
src.close();
dst.close();
}
}} catch (Exception e) {}
}
此照片是在复制我的文件夹,但直到它被压缩。
使用cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
这里outputFile Uri是要保存的文件的位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.