【问题标题】:How to not save image captured with camera in an self made folder?如何不将相机拍摄的图像保存在自制文件夹中?
【发布时间】:2017-06-01 09:10:34
【问题描述】:

大家好!我正在使用将照片保存在手机图库中的相机。但我不想将照片保存在图库中。我想把它保存在别的地方。所以我只需要不要将它自动保存在图库中。

这是我的代码:

public class MainActivity extends AppCompatActivity {

    ImageView photo;

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

        Button click = (Button)findViewById(R.id.btnPhoto);
        photo = (ImageView) findViewById(R.id.imgPhoto);

        click.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(intent, 0);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            Bitmap bitmap = (Bitmap)data.getExtras().get("data");
            photo.setImageBitmap(bitmap);
    }
}

如果我的问题不够好,请问我!请帮帮我。

【问题讨论】:

标签: android android-studio android-intent


【解决方案1】:

这个打开相机和路径的方法是File declare global。

 private void openCamera() {
    Logger.i("openCamera");

    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(this.getPackageManager()) != null) {
        File rootPath = new File(Environment.getExternalStorageDirectory(), "folderName");
        if (!rootPath.exists())
            rootPath.mkdirs();

        DateFormat df = new SimpleDateFormat("yyyy-MM-dd-kk-mm-ss");
        String snapshotImage = df.format(new Date()) + ".jpg";

        rootPath = new File(rootPath + "/" + snapshotImage);
        path = rootPath;

       takePictureIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(path));


        startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
    }

}

得到结果

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

    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 8;



        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
            FileOutputStream fos = null;
            try {

              //  Float Latitude=0.0f, Longitude=0.0f;

                imageDvr.setImageURI(Uri.parse(path.getAbsolutePath()));



            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (fos != null) {
                    try {
                        fos.close();
                    } catch (IOException e) {
                    }
                }
            }


        }


    }


}

并在清单文件中给予权限

 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

【讨论】:

    【解决方案2】:

    您将在onActivityResult()中获得图像的位图,并将该图像按位图存储。

    使用以下功能将图像存储您的自制文件夹。您需要创建该文件夹并需要使用该文件夹的路径。

    像这样。

    public void SaveImage(Bitmap showedImgae){
    
    String root = Environment.getExternalStorageDirectory().toString();
    File myDir = new File(root + "/YourSelfMadeFolder");    
    myDir.mkdirs();
    Random generator = new Random();
    int n = 10000;
    n = generator.nextInt(n);
    String fname = "FILENAME-"+ n +".jpg";
    File file = new File (myDir, fname);
    if (file.exists ()) file.delete (); 
    try {
        FileOutputStream out = new FileOutputStream(file);
        showedImgae.compress(Bitmap.CompressFormat.JPEG, 100, out);
        Toast.makeText(activityname.this, "Image Saved", Toast.LENGTH_SHORT).show();
        out.flush();
        out.close();
    
    } catch (Exception e) {
        e.printStackTrace();
    }
    
    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    Uri contentUri = Uri.fromFile(file);
    mediaScanIntent.setData(contentUri);
    getApplicationContext().sendBroadcast(mediaScanIntent);
    }
    

    【讨论】:

      【解决方案3】:

      您可以使用您的 URI 添加意图 android.provider.MediaStore.EXTRA_OUTPUT:

          private File getOutputMediaFile() {
          File mediaStorageDir = null;
          String state = Environment.getExternalStorageState();
      
              if (state.contains(Environment.MEDIA_MOUNTED)) {
                  mediaStorageDir = new File(Environment
                          .getExternalStorageDirectory().toString() + "/yourFolderName");
              } else {
                  mediaStorageDir = new File(Environment
                          .getExternalStorageDirectory().toString() + "/yourFolderName");
              }
      
          if (!mediaStorageDir.exists()) {
              Logger.d("Desc", "File dir " + mediaStorageDir.mkdirs());
          }
      
          String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
      
      
              mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                      "Image_" + timeStamp + ".jpg");
          return mediaFile;
         }
      

      您的按钮点击:

        click.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
      
              Uri mImageCaptureUri = null;
              File mediaFile = getOutputMediaFile();
              mImageCaptureUri = Uri.fromFile(mediaFile);
      
              intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri);
              intent.putExtra("return-data", true);
              this.startActivityForResult(intent, REQUEST_CODE_TAKE_PICTURE);
      }}0;
      

      关于活动结果:

          @Override
          protected void onActivityResult(int requestCode, int resultCode, Intent data) {
          super.onActivityResult(requestCode, resultCode, data);
      
            Log.d("FilePath","Path"+mediaFile.getAbsolutePath();)
          }
      

      【讨论】:

        猜你喜欢
        • 2012-01-14
        • 1970-01-01
        • 2017-09-20
        • 2021-07-05
        • 2019-06-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多