简体   繁体   English

如何将从相机拍摄的图像保存到内部存储器中

[英]How can I save image taken from camera into internal storage

I have this code that take photo and save to external storage, what I want is save to internal storage, please help me... what I should change to save to internal storage... 我有这段代码可以拍照并保存到外部存储中,我想要保存到内部存储中,请帮助我...我应该更改以保存到内部存储中...

thank you 谢谢

public class MainActivity extends AppCompatActivity {

    public static final int CAPTURE_IMAGE_FULLSIZE_ACTIVITY_REQUEST_CODE = 1777;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button = (Button) findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");

                File file = new File(Environment.getExternalStorageDirectory()+File.separator + "image.jpg");

                intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
                startActivityForResult(intent, CAPTURE_IMAGE_FULLSIZE_ACTIVITY_REQUEST_CODE);

            }
        });
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    //Check that request code matches ours:

        if (requestCode == CAPTURE_IMAGE_FULLSIZE_ACTIVITY_REQUEST_CODE) {
        //Get our saved file into a bitmap object:

            File file = new File(Environment.getExternalStorageDirectory() + File.separator + "image.jpg");
            Bitmap bitmap = decodeSampledBitmapFromFile(file.getAbsolutePath(), 1000, 700);
        }
    }

    public static Bitmap decodeSampledBitmapFromFile(String path, int reqWidth, int reqHeight) { 

        //First decode with inJustDecodeBounds=true to check dimensions

        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, options);

        // Calculate inSampleSize, Raw height and width of image

        final int height = options.outHeight;
        final int width = options.outWidth;
        options.inPreferredConfig = Bitmap.Config.RGB_565;
        int inSampleSize = 1;

        if (height > reqHeight) {
            inSampleSize = Math.round((float) height / (float) reqHeight);
        }
        int expectedWidth = width / inSampleSize;

        if (expectedWidth > reqWidth) {
            //if(Math.round((float)width / (float)reqWidth) > inSampleSize) // If bigger SampSize..

            inSampleSize = Math.round((float) width / (float) reqWidth);
        }

        options.inSampleSize = inSampleSize;

        // Decode bitmap with inSampleSize set

        options.inJustDecodeBounds = false;

        return BitmapFactory.decodeFile(path, options);
    }

}

You can try this code to save your image to internal storage: 您可以尝试使用以下代码将图像保存到内部存储中:

FileOutputStream fos; 
try { 
    fos = openFileOutput(FILENAME, Context.MODE_PRIVATE); 
    // You can also use .JPG
    yourBitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);   
}  
catch (FileNotFoundException e) { 
    Log.e(TAG, e.getMessage());
}  
catch (IOException e) { 
   Log.e(TAG, e.getMessage());
} finally {
    fos.close();
}

You can also have a look at this: https://developer.android.com/guide/topics/data/data-storage.html#filesInternal 您也可以查看以下内容: https : //developer.android.com/guide/topics/data/data-storage.html#filesInternal

EDIT 编辑

For saving your full size image file to internal storage, you'll have to change your 要将完整尺寸的图像文件保存到内部存储中,您必须更改

File file = new File(Environment.getExternalStorageDirectory()+File.separator + "image.jpg");

to

File file = new File(context.getFilesDir(), "image.jpg");

getFilesDir() returns internal directory of your app. getFilesDir()返回应用程序的内部目录。 You will find more detailed information here: https://developer.android.com/training/basics/data-storage/files.html#WriteInternalStorage 您将在此处找到更多详细信息: https : //developer.android.com/training/basics/data-storage/files.html#WriteInternalStorage

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将相机拍摄的图像保存到内部存储器中 - Saving image taken from camera into INTERNAL storage 如何将相机中的图像保存在内存中? - How I can save an image from the camera in the internal memory? 如何保存从相机意图拍摄的图像用作个人资料图像? - How can i save the image taken from camera intent to be used as a profile image? 如何显示从相机拍摄的图像 - How can I show image taken from camera 如何将以相机意图拍摄的图像设置为ImageView? - How can i set an image taken from the Camera intent into a ImageView? 无法将捕获的图像保存到内部存储的android相机 - Can't save captured image to Internal storage android camera 如何用相机拍摄图像并将其保存到手机存储? - How can I capture an image with camera and save it to phone storage? Android WebView:无法将相机拍摄的图像保存或上传到HTML5 ZFCC790C72A86190DE1B549D0C从存储中上传现有图像DDC6F,但可以上传55 - Android WebView: cannot save or upload image taken with camera to HTML5 canvas, but can upload existing images from storage 如何保存从相机拍摄的图像并将其显示到列表视图-发生“ IllegalStateException”崩溃 - how to save image taken from camera and show it to listview - crashes with “IllegalStateException” 从相机拍摄后将图像保存到图库 - save image to the gallery after taken from the camera
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM