2014-04-04 35 views
1

我需要爲不同的圖像設置不同的getPath()。以下是描述一個圖像的getPath的示例。我無法理解如何使用它來設置2張圖像。不同的getPath不同的圖像

public String getPath(Uri uri) { 
     String[] projection = { MediaStore.Images.Media.DATA }; 
     Cursor cursor = managedQuery(uri, projection, null, null, null); 
     if (cursor != null) { 
      // HERE YOU WILL GET A NULLPOINTER IF CURSOR IS NULL 
      // THIS CAN BE, IF YOU USED OI FILE MANAGER FOR PICKING THE MEDIA 
      int column_index = cursor 
        .getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
      cursor.moveToFirst(); 
      return cursor.getString(column_index); 
     } else 
      return null; 
    } 

位圖: -

public void decodeFile(String filePath) { 
     // Decode image size 
     BitmapFactory.Options o = new BitmapFactory.Options(); 
     o.inJustDecodeBounds = true; 
     BitmapFactory.decodeFile(filePath, o); 

     // The new size we want to scale to 
     final int REQUIRED_SIZE = 70; 

     // Find the correct scale value. It should be the power of 2. 
     int width_tmp = o.outWidth, height_tmp = o.outHeight; 
     int scale = 1; 
     while (true) { 
      if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE) 
       break; 
      width_tmp /= 2; 
      height_tmp /= 2; 
      scale *= 2; 
     } 

     // Decode with inSampleSize 
     BitmapFactory.Options o2 = new BitmapFactory.Options(); 
     o2.inSampleSize = scale; 
     bitmap = BitmapFactory.decodeFile(filePath, o2); 
     bitmap2 = BitmapFactory.decodeFile(filePath, o2); 
     imgView.setImageBitmap(bitmap); 

     imgView2.setImageBitmap(bitmap2); 
+0

你的問題不明確。 – GrIsHu

+0

我們需要位圖在按鈕單擊時在imageview上顯示圖像。我試圖通過上面的代碼找到它的路徑。它對1個圖像顯示工作正常,但現在我想顯示2個圖像,我認爲這需要1個更多的路徑來構建。 – WannaBeGeek

+0

@ user3467204,檢查我的下面的答案,希望它會幫助你。 – InnocentKiller

回答

0

無其實你不必使用不同的getPath爲多張圖像,您只能n數量的圖像,

看這有一個方法下面的例子,

//按鈕的點擊事件我是朗雄手機的畫廊

but1.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       openGallery(SELECT_FILE1); 
      } 
     }); 

     but2.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       openGallery(SELECT_FILE2); 
      } 
     }); 

這裏是一個開放的圖庫函數。

public void openGallery(int req_code) { 
     Intent i = new Intent(Intent.ACTION_PICK, 
       android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI); 
     startActivityForResult(i, req_code); 
    } 

和onActivity結果裏面我設置不同的路徑,從我得到的圖像,

public void onActivityResult(int requestCode, int resultCode, Intent data) { 

     if (resultCode == RESULT_OK) { 
      Uri selectedImageUri = data.getData(); 

      if (requestCode == SELECT_FILE1) { 
       selectedPath1 = getPath(selectedImageUri); 
       editText1.setText(selectedPath1); 
      } 
      if (requestCode == SELECT_FILE2) { 
       selectedPath2 = getPath(selectedImageUri); 
       editText2.setText(selectedPath2); 
      } 

     } 
    } 

這裏最後

public String getPath(Uri uri) { 
     String[] projection = { MediaStore.Images.Media.DATA }; 
     @SuppressWarnings("deprecation") 
     Cursor cursor = managedQuery(uri, projection, null, null, null); 
     int column_index = cursor 
       .getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
     cursor.moveToFirst(); 
     return cursor.getString(column_index); 
    }