Get file path of image on Android

后端 未结 8 1749
长发绾君心
长发绾君心 2020-11-30 09:48

I have an app that can make pictures and upload them. The upload requires the file path of the photo but I can\'t get it.

This is my code:

public voi         


        
8条回答
  •  佛祖请我去吃肉
    2020-11-30 10:13

    I am doing this on click of Button.

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

    You can get the URL of the recently Captured Image from variable CapturedImageURL

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

提交回复
热议问题