2013-07-04 52 views
0

我装箱一个图像存储在SD卡中保存图像,然后我需要的图像的图像路径和名称请告诉如何获取图像的名称和路径如何在android中获取文件路径和名称?

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

检查了这一点 http://stackoverflow.com/questions/15432592/get-p ath-of-image-on-android – Tombeau

+0

谢谢工作.......... –

回答

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

      } 

    } 
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); 
相关问题