簡體   English   中英

Android-如何存儲和從內部存儲檢索圖像

[英]Android - How to store and retrieve the image from internal storage

我想從相機拍攝圖像並將其存儲並從內部存儲中查看。

我使用此代碼從相機拍攝圖像。

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, TAKE_PIC);

我嘗試了以下方法將位圖保存在內部存儲中。

public String saveToInternalSorage(Bitmap bitmapImage, String fileName){       

        File file = new File(createBasePath(),  fileName + ".png");
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(file);
            bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return file.getName();
    }

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == TAKE_PIC && resultCode == RESULT_OK) {
            Intent i=new Intent(Intent.ACTION_VIEW);
            i.setDataAndType(Uri.fromFile(output), "image/png");
            startActivity(i);
        }
    }

我的路徑是/data/user/0/com.dev/app/1454351400000/33352/capture_Image1.png但是這里我面臨一個問題。 位圖的大小非常小。 我想要從相機拍攝的確切圖像。

然后我找到了一種解決方案。 在意圖之前設置路徑。 所以我嘗試了這樣。

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
 output = new File(createBasePath(), "capture_Image1" + ".png"); 
 intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(output));
 startActivityForResult(intent, TAKE_PIC);

當我使用此代碼時,圖像未顯示。 onActivityResultresultCode表示-1。

如果我使用以下代碼

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
 File dir= Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
 output=new File(dir, "CameraContentDemo.png");
 intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(output));
 startActivityForResult(intent, TAKE_PIC);

這段代碼工作正常。 拍攝圖像后顯示圖像。

因此,請讓我知道以原始質量存儲和檢索來自內部db的圖像。

   Below is code to save the image to internal directory.

    private String saveToInternalStorage(Bitmap bitmapImage){
            ContextWrapper cw = new ContextWrapper(getApplicationContext());
             // path to imageDir
            File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
            // Now Create imageDir
            File mypath=new File(directory,"abc.jpg");

            FileOutputStream foss = null;
            try {           
                foss = new FileOutputStream(mypath);
           // Using compress method on the BitMap object to write image to the OutputStream
                bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, foss);
            } catch (Exception e) {
                  e.printStackTrace();
            } finally {
                  foss.close(); 
            } 
            return directory.getAbsolutePath();
        }


Use below code for Reading a file from internal storage

private void loadImageFromStorage(String path)
{

    try {
        File f=new File(path, "abc.jpg");
        Bitmap b = BitmapFactory.decodeStream(new FileInputStream(f));
            ImageView img=(ImageView)findViewById(R.id.imgPicker);
        img.setImageBitmap(b);
    } 
    catch (FileNotFoundException e) 
    {
        e.printStackTrace();
    }

}
void openMultiSelectGallery() {
      Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri =getOutputMediaFile();
        intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
        /* start activity for result pass intent as argument and request code */
        startActivityForResult(intent, CAMERA_REQUEEST_CODE);

    }
/**
     * This method set the path for the captured image from camera for updating
     * the profile pic
     */
    private Uri getOutputMediaFile() {

        File mediaStorageDir = new File(
                Environment.getExternalStorageDirectory(), "."
                + Constants.CONTAINER);

        // Create the storage directory if it does not exist
        if (!mediaStorageDir.exists()) {
            mediaStorageDir.mkdirs();
        }

        File mediaFile = new File(mediaStorageDir.getPath() + File.separator
                + "IMG_" + System.currentTimeMillis() + ".png");
        Uri uri = null;
        if (mediaFile != null) {
            uri = Uri.fromFile(mediaFile);

        }
        return uri;
    }

     @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        super.onActivityResult(requestCode, resultCode, data);
 if (resultCode == RESULT_OK
                && requestCode == CAMERA_REQUEEST_CODE) {
         String path =fileUri.getPath();
         //decode the path to bitmap here

        } 

        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM