在Android上获取图像的文件路径

我有一个可以制作图片并上传它们的应用程序。 上传需要照片的文件路径,但我无法得到它。

这是我的代码:

public void maakfoto (View v) { Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, mImageCaptureUri); startActivityForResult(cameraIntent, CAMERA_REQUEST); } protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) { Bitmap photo = (Bitmap) data.getExtras().get("data"); imageView.setImageBitmap(photo); knop.setVisibility(Button.VISIBLE); System.out.println(mImageCaptureUri); } } 

请帮我看一下文件路径。

谢谢

  • Phonegap cordova android'项目已存在'或运行时错误
  • “android:找不到sdkmanager.jar”
  • 获取Uri Android的真实路径
  • 如何将android添加到cordova平台? PATH错误?
  • Android沿路径移动对象
  • 不需要的Warp效果在路径android上绘制文本
  • 如何序列化android.graphics.Path的对象
  • Android数组的路径 - 读取路径上的点?
  • 发布到Twitter需要在要发布的请求中发送设备上的图像实际路径。 我发现获得实际路径并不困难,而且往往我会走错路。

    为了解决这个问题,一旦你有了Bitmap ,我就用它来获取使用getImageUri()的URI。 随后,我使用tempUri Uri实例来获取设备上的实际路径。

    这是生产代码并经过自然测试。 😉

     protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) { Bitmap photo = (Bitmap) data.getExtras().get("data"); imageView.setImageBitmap(photo); knop.setVisibility(Button.VISIBLE); // CALL THIS METHOD TO GET THE URI FROM THE BITMAP Uri tempUri = getImageUri(getApplicationContext(), photo); // CALL THIS METHOD TO GET THE ACTUAL PATH File finalFile = new File(getRealPathFromURI(tempUri)); System.out.println(mImageCaptureUri); } } public Uri getImageUri(Context inContext, Bitmap inImage) { ByteArrayOutputStream bytes = new ByteArrayOutputStream(); inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes); String path = Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null); return Uri.parse(path); } public String getRealPathFromURI(Uri uri) { Cursor cursor = getContentResolver().query(uri, null, null, null, null); cursor.moveToFirst(); int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA); return cursor.getString(idx); } 

    为了拍摄照片,您必须确定您希望保存图像的路径,并将其作为意图中的额外内容传递,例如:

     private void capture(){ String directoryPath = Environment.getExternalStorageDirectory() + "/" + IMAGE_DIRECTORY + "/"; String filePath = directoryPath+Long.toHexString(System.currentTimeMillis())+".jpg"; File directory = new File(directoryPath); if (!directory.exists()) { directory.mkdirs(); } this.capturePath = filePath; // you will process the image from this path if the capture goes well Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra( MediaStore.EXTRA_OUTPUT, Uri.fromFile( new File(filePath) ) ); startActivityForResult(intent, REQUEST_CAPTURE); } 

    我只是从我给出的另一个答案中复制了上述部分。

    但是,要警告您,您应该注意的设备之间的图像捕获行为存在很多不一致之处。

    这是我在一些HTC设备上遇到的一个问题,它会保存在我通过的位置,并在其默认位置导致设备上出现重复图像: 在拍摄相机意图照片后删除图库图像

    试试mImageCaptureUri.getPath(); 通过以下方式:

     if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) { //Get your Image Path String Path=mImageCaptureUri.getPath(); Bitmap photo = (Bitmap) data.getExtras().get("data"); imageView.setImageBitmap(photo); knop.setVisibility(Button.VISIBLE); System.out.println(mImageCaptureUri); } 

    我点击按钮就是这样做的。

     private static final int CAMERA_PIC_REQUEST = 1; private View.OnClickListener OpenCamera=new View.OnClickListener() { @Override public void onClick(View paramView) { // TODO Auto-generated method stub Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); NewSelectedImageURL=null; //outfile where we are thinking of saving it Date date = new Date(); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss"); String newPicFile = RecipeName+ df.format(date) + ".png"; String outPath =Environment.getExternalStorageDirectory() + "/myFolderName/"+ newPicFile ; File outFile = new File(outPath); CapturedImageURL=outFile.toString(); Uri outuri = Uri.fromFile(outFile); cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, outuri); startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST); } }; 

    您可以从variablesCapturedImageURL获取最近捕获的图像的URL

     protected void onActivityResult(int requestCode, int resultCode, Intent data) { ////////////////////////////////////// if (requestCode == CAMERA_PIC_REQUEST) { // do something if (resultCode == RESULT_OK) { Uri uri = null; if (data != null) { uri = data.getData(); } if (uri == null && CapturedImageURL != null) { uri = Uri.fromFile(new File(CapturedImageURL)); } File file = new File(CapturedImageURL); if (!file.exists()) { file.mkdir(); sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+Environment.getExternalStorageDirectory()))); } } } 

    使用此函数来获取捕获图像路径

     protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) { Uri mImageCaptureUri = intent.getData(); Bitmap photo = (Bitmap) data.getExtras().get("data"); imageView.setImageBitmap(photo); knop.setVisibility(Button.VISIBLE); System.out.println(mImageCaptureUri); //getImgPath(mImageCaptureUri);// it will return the Capture image path } } public String getImgPath(Uri uri) { String[] largeFileProjection = { MediaStore.Images.ImageColumns._ID, MediaStore.Images.ImageColumns.DATA }; String largeFileSort = MediaStore.Images.ImageColumns._ID + " DESC"; Cursor myCursor = this.managedQuery( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, largeFileProjection, null, null, largeFileSort); String largeImagePath = ""; try { myCursor.moveToFirst(); largeImagePath = myCursor .getString(myCursor .getColumnIndexOrThrow(MediaStore.Images.ImageColumns.DATA)); } finally { myCursor.close(); } return largeImagePath; } 

    简单传递意图首先

     Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(i, RESULT_LOAD_IMAGE); 

    你将获得关于onActivityResult的图片路径

     @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) { Uri selectedImage = data.getData(); String[] filePathColumn = { MediaStore.Images.Media.DATA }; Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null); cursor.moveToFirst(); int columnIndex = cursor.getColumnIndex(filePathColumn[0]); String picturePath = cursor.getString(columnIndex); cursor.close(); ImageView imageView = (ImageView) findViewById(R.id.imgView); imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath)); } } 

    要获取android中所有图像的路径,我使用以下代码

     public void allImages() { ContentResolver cr = getContentResolver(); Cursor cursor; Uri allimagessuri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; String selection = MediaStore.Images.Media._ID + " != 0"; cursor = cr.query(allsongsuri, STAR, selection, null, null); if (cursor != null) { if (cursor.moveToFirst()) { do { String fullpath = cursor.getString(cursor .getColumnIndex(MediaStore.Images.Media.DATA)); Log.i("Image path ", fullpath + ""); } while (cursor.moveToNext()); } cursor.close(); } }