0

我只想將圖片保存在手機中的Imagefolder中。 我有2個我嘗試過的例子。Android:將拍攝的照片保存到Imagefolder

1例

我的應用程序崩潰時我激活的onClick方法:

public void onClick(View arg0) { 

     Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 

     startActivityForResult(cameraIntent, 1337); 
}}); 

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

    if(requestCode == 1337) 
      { 
       Bitmap thumbnail = (Bitmap) data.getExtras().get("data"); 

MediaStore.Images.Media.insertImage(getContentResolver(),file.getAbsolutePath(),file.getName(),file.getName()); 

      } 
      else 
      { 
       Toast.makeText(AndroidCamera.this, "Picture NOt taken", Toast.LENGTH_LONG); 
      } 
      super.onActivityResult(requestCode, resultCode, data); 
     } 

2例

之前,我救了我的拍攝圖片與URI。但它將我的照片保存在一個文件夾中,我只能在我的PC或FileApp上訪問該文件夾。我不知道如何使用Uri將路徑方向更改爲手機中現有的默認圖像文件夾。

Uri uriTarget = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, new ContentValues()); 

回答

1

這是我如何與影像保存到指定的imagefolder管理

當啓動相機的意圖,我定義路徑和目錄,我的形象應該被保存,並通過以此爲intetn額外啓動相機時:

private void startCameraIntent() { 
     //create file path 
     final String photoStorePath = getProductPhotoDirectory().getAbsolutePath(); 

     //create file uri 
     final Uri fileUri = getPhotoFileUri(photoStorePath); 

     //create camera intent 
     final Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 

     //put file ure to intetn - this will tell camera where to save file with image 
     cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); 
     // start activity 
     startActivityForResult(cameraIntent, REQUEST_CODE_PHOTO_FROM_CAMERA); 

     //start image scanne to add photo to gallery 
     addProductPhotoToGallery(fileUri); 
    } 

這裏有一些在代碼中使用上述

private File getProductPhotoDirectory() { 
     //get directory where file should be stored 
     return new File(Environment.getExternalStoragePublicDirectory(
       Environment.DIRECTORY_PICTURES), 
       "myPhotoDir"); 
    } 

    private Uri getPhotoFileUri(final String photoStorePath) { 

     //timestamp used in file name 
     final String timestamp = new SimpleDateFormat("yyyyMMdd_HHmmss", 
       Locale.US).format(new Date()); 

     // file uri with timestamp 
     final Uri fileUri = Uri.fromFile(new java.io.File(photoStorePath 
       + java.io.File.separator + "IMG_" + timestamp + ".jpg")); 

     return fileUri; 
    } 

    private void addProductPhotoToGallery(Uri photoUri) { 
     //create media scanner intetnt 
     Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); 

     //set uri to scan 
     mediaScanIntent.setData(photoUri); 
     //start media scanner to discover new photo and display it in gallery 
     this.sendBroadcast(mediaScanIntent); 
    } 
+0

泰輔助方法爲你的答案。那只是打開了默認的相機。我有一個疊加預覽相機,通過拍照消失。我不確切知道Intent相機在做什麼,但我認爲,我必須沒有它呢? :) – csnewb

+0

是的,如果您的應用程序正在管理相機,則無需啓動默認應用程序。 –

相關問題