0

我在 sd 卡中创建了一个图像存储以保存该图像,然后我需要该图像路径和图像名称请告诉如何获取图像的名称和路径

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

2 回答 2

2

我懂了

 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)
            {

            }

    }
于 2014-04-11T10:46:55.910 回答
1

你可以这样尝试

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

File f = new File(storedimagepath+"/photo.jpg");
Bitmap bmp = BitmapFactory.decodeFile(f.getAbsolutePath());
ImageView mImgView1 = (ImageView)findViewById(R.id.imageView);
mImgView1.setImageBitmap(bmp);
于 2013-07-04T06:41:18.027 回答