简体   繁体   English

Android Studio Camera App保存到内部存储

[英]Android Studio Camera App Saving to Internal Storage

Currently looking for help on saving images from a camera app to internal storage do to Nexus not having an SD card, our current code saves the photos taken to an SD card folder and the quality is not good. 当前正在寻求有关将相机应用程序中的图像保存到内部存储的帮助,因为Nexus没有SD卡,因此我们当前的代码会将所保存的照片保存到SD卡文件夹中,因此质量不佳。

public class MainActivity extends ActionBarActivity {

    private ImageView imageHolder;
    private final int requestCode = 20;
    public final static String EXTRA_MESSAGE = "com.test1.cam.camapp.MESSAGE";



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

        imageHolder = (ImageView)findViewById(R.id.captured_photo);
        Button capturedImageButton = (Button)findViewById(R.id.photo_button);
        capturedImageButton.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent photoCaptureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(photoCaptureIntent, requestCode);
            }
        });
    }

    @Override

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(this.requestCode == requestCode && resultCode == RESULT_OK){
            Bitmap bitmap = (Bitmap)data.getExtras().get("data");

            String partFilename = currentDateFormat();
           storeCameraPhotoInSDCard(bitmap, partFilename);




            // display the image from SD Card to ImageView Control
            String storeFilename = "photo_" + partFilename + ".jpg";
            Bitmap mBitmap = getImageFileFromSDCard(storeFilename);
            imageHolder.setImageBitmap(mBitmap);
        }
    }

    public void showGreetings(View view)
    {

        String button_text;
        button_text = ((Button) view) .getText().toString();
        if(button_text.equals("Info"))
        {

            Intent intent = new Intent(this, SecondActivity.class);
            startActivity(intent);
        }
        else if (button_text.equals("Info"))
        {
            Intent intent = new Intent(this, SecondActivity.class);
            startActivity(intent);
        }



    }
    public void saveImage(Context context, Bitmap b,String name,String extension){
        name=name+"."+extension;
        FileOutputStream out;
        try {
            out = context.openFileOutput(name, Context.MODE_PRIVATE);
            b.compress(Bitmap.CompressFormat.JPEG, 90, out);
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private String currentDateFormat(){
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd_HH_mm_ss");
        String  currentTimeStamp = dateFormat.format(new Date());
        return currentTimeStamp;
    }


    private void storeCameraPhotoInSDCard(Bitmap bitmap, String currentDate){
        File outputFile = new File(Environment.getExternalStorageDirectory(), "photo_" + currentDate + ".jpg");
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(outputFile);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);
            fileOutputStream.flush();
            fileOutputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private Bitmap getImageFileFromSDCard(String filename){
        Bitmap bitmap = null;
        File imageFile = new File(Environment.getExternalStorageDirectory() + filename);
        try {
            FileInputStream fis = new FileInputStream(imageFile);
            bitmap = BitmapFactory.decodeStream(fis);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return bitmap;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

To improve the image quality you should change compression to PNG, or change the second parameters to 100 (PNG is lossless and will ignore second params). 为了提高图像质量,您应该将压缩更改为PNG,或者将第二个参数更改为100(PNG是无损的,将忽略第二个参数)。

        b.compress(Bitmap.CompressFormat.PNG, 100, out);

To change external into internal just change 将外部变成内部只是改变

File outputFile = new File(Environment.getExternalStorageDirectory(), "photo_" + currentDate + ".jpg");

into 进入

File outputFile = new File(context.getFilesDir(), "photo_" + currentDate + ".jpg");

The image you are trying to save returns you jest the thumbnail of the actual image that is why you are getting low quality image. 您尝试保存的图像会返回实际图像的缩略图,这就是为什么您获得低质量图像的原因。 You should pass the image name to the intent to save the high quality image when it is captured Following Helper class that I use for image capture may be of some help to you 您应该将图像名称传递给保存高质量图像的意图,以便在捕获图像后使用以下用于图像捕获的Helper类可能对您有所帮助

public class CaptureImageHelper {

    private static final int DEFAULT_WIDTH = 1024;        // min pixels
    private static final int DEFAULT_HEIGHT = 768;        // min pixels
    private static final String TEMP_IMAGE_NAME = "tempImage";


    public static Intent getImageCaptureIntent(Context context, String title) {
        Intent chooserIntent = null;
        List<Intent> intentList = new ArrayList<>();
        Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(getTempFile(context)));
        intentList = addIntentsToList(context, intentList, takePhotoIntent);
        if (intentList.size() > 0) {
            chooserIntent = Intent.createChooser(intentList.remove(intentList.size() - 1), title);
            chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentList.toArray(new Parcelable[]{}));
        }
        return chooserIntent;

    }

    private static File getTempFile(Context context) {
        //Note you can change the path here according to your need
        File imageFile = new File(Environment.getExternalStorageDirectory(), TEMP_IMAGE_NAME);
        imageFile.getParentFile().mkdirs();
        return imageFile;
    }

    private static List<Intent> addIntentsToList(Context context, List<Intent> list, Intent intent) {
        List<ResolveInfo> resInfo = context.getPackageManager().queryIntentActivities(intent, 0);
        for (ResolveInfo resolveInfo : resInfo) {
            String packageName = resolveInfo.activityInfo.packageName;
            Intent targetedIntent = new Intent(intent);
            targetedIntent.setPackage(packageName);
            list.add(targetedIntent);
        }
        return list;
    }

    public static Bitmap getImageFromResult(Context context, int resultCode, Intent imageReturnedIntent) {
        return getImageFromResult(context, DEFAULT_WIDTH, DEFAULT_HEIGHT, resultCode, imageReturnedIntent);
    }

    public static Bitmap getImageFromResult(Context context, int width, int height, int resultCode, Intent imageReturnedIntent) {
        Bitmap bm = null;
        if (resultCode == Activity.RESULT_OK) {
            Uri selectedImage;
            File imageFile = getTempFile(context);
            selectedImage = Uri.fromFile(imageFile);
            bm = getImageResized(context, selectedImage, width, height);
            int rotation = getRotation(context, selectedImage, true);
            bm = rotate(bm, rotation);
        }
        return bm;
    }

    private static Bitmap getImageResized(Context context, Uri selectedImage, int reqWidth, int reqHeight) {
        // First decode with inJustDecodeBounds=true to check dimensions
        System.gc();
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        Bitmap actuallyUsableBitmap = null;
        AssetFileDescriptor fileDescriptor = null;
        try {
            fileDescriptor = context.getContentResolver().openAssetFileDescriptor(selectedImage, "r");
        } catch (FileNotFoundException e) {
        }
        if (null != fileDescriptor) {
            BitmapFactory.decodeFileDescriptor(fileDescriptor.getFileDescriptor(), null, options);
            // Calculate inSampleSize
            options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
            // Decode bitmap with inSampleSize set
            options.inJustDecodeBounds = false;
            actuallyUsableBitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor.getFileDescriptor(), null, options);
        }
        return actuallyUsableBitmap;
    }

    private static Bitmap getImageResized(Context context, Uri selectedImage) {
        return getImageResized(context, selectedImage, DEFAULT_WIDTH, DEFAULT_HEIGHT);
    }

    private static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {

            final int halfHeight = height / 2;
            final int halfWidth = width / 2;

            // Calculate the largest inSampleSize value that is a power of 2 and keeps both
            // height and width larger than the requested height and width.
            while ((halfHeight / inSampleSize) > reqHeight
                    && (halfWidth / inSampleSize) > reqWidth) {
                inSampleSize *= 2;
            }
        }

        return inSampleSize;
    }

    private static int getRotation(Context context, Uri imageUri, boolean isCamera) {
        int rotation;
        if (isCamera) {
            rotation = getRotationFromCamera(context, imageUri);
        } else {
            rotation = getRotationFromGallery(context, imageUri);
        }
        return rotation;
    }

    private static int getRotationFromCamera(Context context, Uri imageFile) {
        int rotate = 0;
        try {
            context.getContentResolver().notifyChange(imageFile, null);
            ExifInterface exif = new ExifInterface(imageFile.getPath());
            int orientation = exif.getAttributeInt(
                    ExifInterface.TAG_ORIENTATION,
                    ExifInterface.ORIENTATION_NORMAL);

            switch (orientation) {
                case ExifInterface.ORIENTATION_ROTATE_270:
                    rotate = 270;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    rotate = 180;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_90:
                    rotate = 90;
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rotate;
    }

    private static int getRotationFromGallery(Context context, Uri imageUri) {
        int orientation = 0;
        String[] columns = {MediaStore.Images.Media.ORIENTATION};
        Cursor cursor = context.getContentResolver().query(imageUri, columns, null, null, null);
        if (null != cursor && cursor.moveToFirst()) {
            int orientationColumnIndex = cursor.getColumnIndex(columns[0]);
            orientation = cursor.getInt(orientationColumnIndex);
            cursor.close();
        }
        return orientation;
    }

    private static Bitmap rotate(Bitmap bm, int rotation) {
        if (rotation != 0) {
            Matrix matrix = new Matrix();
            matrix.postRotate(rotation);
            return Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);
        }
        return bm;
    }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM