Android - 获取路径文件

时间:2014-02-17 13:01:21

标签: android file camera

我想知道如何从其名称中获取文件。

我用相机拍照。它保存了照片:

String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
File mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");

现在(在关闭并再次打开应用程序之后),我想从图片的名称(我将其保存在字符串(timeStamp)中)获取文件或Uri到预览。

抱歉我的英文。

1 个答案:

答案 0 :(得分:2)

它对我有用!!! 希望它对你有用..

          private Uri imageUri;

      public void onClick(View arg0) {
        switch (arg0.getId()) {     
          case R.id.btnImageCapture:

        Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
        File photo = new File(Environment.getExternalStorageDirectory(),  "Pic.jpg");
        intent.putExtra(MediaStore.EXTRA_OUTPUT,
                Uri.fromFile(photo));
        imageUri = Uri.fromFile(photo);
        startActivityForResult(intent, OPEN_CAMERA);
                        break;
        }
    }

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
    case OPEN_CAMERA:
        if (resultCode == Activity.RESULT_OK) {
            Uri selectedImage = imageUri;
            getContentResolver().notifyChange(selectedImage, null);
            ImageView imageView = (ImageView) findViewById(R.id.ImageView);
            ContentResolver cr = getContentResolver();
            Bitmap bitmap;
            try {
                 bitmap = android.provider.MediaStore.Images.Media
                 .getBitmap(cr, selectedImage);

                imageView.setImageBitmap(bitmap);
                Toast.makeText(this, selectedImage.toString(),
                        Toast.LENGTH_LONG).show();

            } catch (Exception e) {
                Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT)
                        .show();

            }
        }
    }
}

回复任何查询