Search code examples
androidandroid-imageandroid-file

how to get the file path and name in android?


I crated one image store in sd card to save that image and then i need that image path and name of the image pls tell how to get the name and path of the image

      public void saveBitmap(Bitmap bmp)
 {
     String file_path = Environment.getExternalStorageDirectory().getAbsolutePath() + 
                                 "/NewFolder";
         File dir = new File(file_path);
         if(!dir.exists())
            dir.mkdirs();
         File file = new File(dir, "myImage.png");




         FileOutputStream fOut;
        try {
            fOut = new FileOutputStream(file);


             bmp.compress(Bitmap.CompressFormat.PNG, 85, fOut);
             fOut.flush();
             fOut.close();

         String name =   Environment.getExternalStorageDirectory().getAbsolutePath() + 
             "/NewFolder";
         storedimagepath=name.toString();



        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

Solution

  • i got this

     void getImageCAMERandGALLERY()
        {
    
            final String [] items = new String[] { "Take from Camera" , "Select from Gallery" };
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this , android.R.layout.select_dialog_item , items);
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
    
            builder.setTitle("Select Image");
            builder.setAdapter(adapter , new DialogInterface.OnClickListener() {
                private Uri mImageCaptureUri;
    
                @Override
                public void onClick(DialogInterface dialog , int item)
                    {
    
                        if (item == 0)
                            {
                                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    
                                mImageCaptureUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory() , "tmp_avatar_"
                                        + String.valueOf(System.currentTimeMillis()) + ".jpg"));
    
                                intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT , mImageCaptureUri);
    
                                try
                                    {
                                        intent.putExtra("return-data" , true);
                                        startActivityForResult(intent , PICK_FROM_CAMERA);
                                    } catch (ActivityNotFoundException e)
                                    {
                                        e.printStackTrace();
                                    }
                            } else
                            {
                                Intent intent = new Intent();
                                intent.setType("image/*");
                                intent.setAction(Intent.ACTION_GET_CONTENT);
                                startActivityForResult(Intent.createChooser(intent , "Complete action using") , PICK_FROM_FILE);
                            }
                    }
            });
    
            final AlertDialog dialog = builder.create();
    
            dialog.show();
    
        }
    
    @Override
    protected void onActivityResult(int requestCode , int resultCode , Intent data)
        {
            if (resultCode != RESULT_OK)
                return;
    
            switch (requestCode) {
                case PICK_FROM_CAMERA:
    
    
                    break;
    
                case PICK_FROM_FILE:
                    mImageCaptureUri = data.getData();
    
    
    
                    break;
    
                case CROP_FROM_CAMERA:
                    Bundle extras = data.getExtras();
    
                    if (extras != null)
                        {
                            Bitmap photo = extras.getParcelable("data");
    
                            UserImage.setImageBitmap(photo);
                            try
                                {
    
                                    Uri tempUri = getImageUri(getApplicationContext() , photo);
                                    storeUriInFile(tempUri);
                                } catch (Exception e)
                                {
                                    e.printStackTrace();
                                }
    
                        }
    
            }
    
        }
    
    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);
        }
    
    
    void storeUriInFile(Uri uri)
        {
            try
                {
                    Bitmap my_btmp = Media.getBitmap(this.getContentResolver() , uri); // BitmapFactory.decodeStream(BufferedInputStream);
                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
                    my_btmp.compress(CompressFormat.PNG , 0 , bos);
                    byte [] bitmapdata = bos.toByteArray();
                    long timeinmilliseconds = new Date().getTime();
                    // store byte array in a image file
                    String filepath = Environment.getExternalStorageDirectory().getPath();
                    File file = new File(filepath , SpeakerBox_FOLDER);
                    if (!file.exists())
                        file.mkdirs();
    
                    String mFileName = file.getAbsolutePath() + "/" + System.currentTimeMillis() + ".png";
                    OutputStream out = new FileOutputStream(mFileName);
                    out.write(bitmapdata);
                    out.flush();
                    out.close();
                    String filename = mFileName;
                    storedimagepath = filename;
    
    
                } catch (Exception e)
                {
    
                }
    
        }