Android图片保存到内部存储是缩略图

标签 android

我制作了一个类来拍摄照片并将其保存到内部存储器,但问题是我保存的是缩略图而不是全尺寸照片。

这是类:

public class PhotoManager {

private final static String DEBUG_TAG = "PHOTOMANAGER";
private String mCurrentPhotoPath = "";
private String filePath = "";
private Intent takePictureIntent;
private Activity activity;
private String type;
private int Q_ID;
private PhotoCallbackListener mListener;
private FragmentManager fragManager;

public void setListener(PhotoCallbackListener listener) {
    mListener = listener;
}

public PhotoManager(Activity activity, int Q_ID, String type, FragmentManager fm, PhotoCallbackListener listener) {
    this.activity = activity;
    this.Q_ID = Q_ID;
    this.type = type;
    this.mListener = listener;
    this.fragManager = fm;
}
// init la vue de la camera
public void init(){
    dispatchTakePictureIntent(11);
}
// affiche la camera
private void dispatchTakePictureIntent(final int actionCode) {


    Fragment f = new Fragment() {
        @Override
        public void onAttach(Activity activity) {
            super.onAttach(activity);
            takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

            startActivityForResult(takePictureIntent, actionCode);
        }

        @Override
        public void onActivityResult(int requestCode, int resultCode,
                                     Intent data) {
            if (data != null) {
                try {
                    handleSmallCameraPhoto(data);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                // efface la photo créee de la galerie
                activity.getContentResolver().delete(data.getData(), null, null);
            }

        }
    };
    FragmentTransaction fragmentTransaction = this.fragManager
            .beginTransaction();
    fragmentTransaction.add(f, "getpicture");
    fragmentTransaction.commit();


}

private void handleSmallCameraPhoto(Intent intent) throws IOException {
    Bundle extras = intent.getExtras();
    Bitmap mImageBitmap = (Bitmap) extras.get("data");

    File f = createImageFile();
    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));

    filePath = saveToInternalStorage(mImageBitmap);

    galleryAddPic();

    mListener.callback(mImageBitmap, filePath);

}

private File createImageFile() throws IOException {
    // Create an image file name
    Log.d(DEBUG_TAG,"createImageFile");
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date(0));
    String imageFileName = timeStamp + "_";
    File image = File.createTempFile(
            imageFileName,
            ".jpg",
            null //default location for temporary files
    );
    mCurrentPhotoPath = image.getAbsolutePath();
    Log.d(DEBUG_TAG,"return "+image.getAbsolutePath());

    return image;
}

private String saveToInternalStorage(Bitmap bitmapImage){

    ContextWrapper cw = new ContextWrapper(activity.getApplicationContext());

    Log.d(DEBUG_TAG,"saveToInternalStorage");

    File directory = cw.getDir("imageDir_"+type, Context.MODE_PRIVATE);
    String fileName = "q"+Q_ID+".jpg";
    // Create imageDir
    File mypath = new File(directory, fileName);

    FileOutputStream fos = null;
    try {

        fos = new FileOutputStream(mypath);

        // Use the compress method on the BitMap object to write image to the OutputStream
        bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
        fos.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    Log.d(DEBUG_TAG,"return "+directory.getAbsolutePath());
    return directory.getAbsolutePath();
}



private void galleryAddPic() {
    File f = new File(mCurrentPhotoPath);
    Uri contentUri = Uri.fromFile(f);
    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,contentUri);
    activity.sendBroadcast(mediaScanIntent);
}
}

我是这样使用的:

pm = new PhotoManager(getActivity(), Q_ID, getArguments().getString("type"), fm, new PhotoCallbackListener() {
        @Override
        public void callback(Bitmap mImageBitmap, String file) {

            imagePreview.setImageBitmap(mImageBitmap);
            filePath = file;

        }

    });

 Button buttonPhoto = (Button) view.findViewById(R.id.q4BtnPhoto);
    buttonPhoto.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            pm.init();
        }

    });

我返回位图缩略图以将其放入布局中,而文件路径仅用于测试。

谁能帮我理解为什么没有保存完整尺寸的照片?

谢谢!

最佳答案

自己解决了:

我犯了一个大错误,我将缩略图保存到内部存储而不是完整尺寸。

在 handleSmallCameraPhoto 方法中,我可以获得完整尺寸的已保存图像:

Uri fullsizeImage = intent.getData();

我得到图像的位图是这样的:

Bitmap mBitmap = MediaStore.Images.Media.getBitmap(activity.getContentResolver(), fullsizeImage);

然后在我调用我的 saveToInternalStorage 方法之后。

关于Android图片保存到内部存储是缩略图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27206859/

相关文章:

android - 图像和 css 没有出现在 apache cordova 中

Android - 在模拟器上错误测试 GPS

java - Google Ads 会中断该应用。减慢帧率

android - 动态壁纸背景的视差效果滚动

android - google maps api for android 注册页面

android - 使用 actionscript3 检索 android 的 IMEI

android - 带有 NestedScrollView 的 CoordinatorLayout 不会使用 adjustResize 调整大小

Android 虚拟设备不工作

java - 相同的异步任务但 onPostExecute() 中的不同结果处理

java - 当再次新选择同一项目时, ListView 中项目的属性更改(或设置为新属性)