0

我有這段代碼可以拍照並保存到外部存儲器,我想要的是保存到內部存儲器,請幫助我...我應該更改以保存到內部存儲...如何將從相機拍攝的圖像保存到內部存儲器

謝謝

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); 
    } 

} 
+0

https://developer.android.com/training/basics/data-storage/files.html –

+0

歡迎來到Stack Overflow!您可能想要修復代碼示例的格式。它沒有出來。 –

回答

0

你可以試試這個代碼將圖像保存到內部存儲:

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(); 
} 

您也可以看看這個:https://developer.android.com/guide/topics/data/data-storage.html#filesInternal

編輯

爲了節省您的最大尺寸的圖像文件的內部存儲,你必須改變你的

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

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

getFilesDir()返回內部目錄的應用程序。你會在這裏找到更多的詳細信息:https://developer.android.com/training/basics/data-storage/files.html#WriteInternalStorage

+0

謝謝,但這隻保存一個位圖,我怎樣才能保存全尺寸的圖像? – edwn

+0

要訪問內部存儲,可以使用getFilesDir()而不是Environment.getExternalStorageDirectory()。看我的編輯。 – Preader

相關問題