2013-08-25 73 views
0

我試圖從圖庫中插入圖像的圖像,但我只看到空白。沒有錯誤。無法從庫中加載圖像

protected static final int RESULT_LOAD_IMAGE = 1; 
ImageView imgfto; 
private static int CAPTURA_FOTO = 2; 
private static int SELECT_FOTO = 1; 
private String fnombre = ""; 

    @Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_insertarlugar); 

    imgfto = (ImageView) findViewById(R.id.imgFoto); 
    EditText titlugar; 
    EditText desclugar; 

      titlugar = (EditText) findViewById(R.id.edit_titulo_lugar); 
    String tlugar = titlugar.getText().toString(); 

    desclugar = (EditText) findViewById(R.id.edit_descripcion_lugar); 
    String dlugar = desclugar.getText().toString(); 

    imgfto.setOnClickListener(new OnClickListener() { 

      @Override 
    public void onClick(View arg0) { 
      final String[] items = {"Cámara", "Galería"}; 

      AlertDialog.Builder builder = new  
        AlertDialog.Builder(Insertarlugar.this); 
      builder.setTitle("Foto"); 
      builder.setItems(items, new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int item) { 

        int cdg = 0; 
        Intent itn = null; 
          switch (item) 
        { 
         case 1: 
         { 
          itn = new Intent(Intent.ACTION_PICK, 
             android.provider.MediaStore.Images.Media. 
             INTERNAL_CONTENT_URI); 
         cdg = SELECT_FOTO; 
          break; 
         } 
           case 2: 
            { 
             itn = new intent(MediaStore.ACTION_IMAGE_ 
             CAPTURE); 
          cdg = CAPTURA_FOTO; 
          fnombre = 
             Environment.getExternalStorageDirectory() + 
             "/Foto.jpg"; 
          Uri fichero = Uri.fromFile(new File(fnombre)); 
          itn.putExtra(MediaStore.EXTRA_OUTPUT, fichero); 
          break; 
         } 
        } 
          startActivityForResult(itn, cdg); 
        Toast.makeText(Insertarlugar.this, "Click\n" + item, 
          Toast.LENGTH_SHORT).show(); 

       } 
      }); 
      AlertDialog alert = builder.create(); 
      alert.show(); 
     } 
       }); 
     } 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 


    if(requestCode == CAPTURA_FOTO) 
    { 
     Uri imgselect = data.getData(); 
     String imgpath = imgselect.getPath(); 
       File f = new File (imgpath); 
       Bitmap bm = BitmapFactory.decodeFile(f.getAbsolutePath()); 
       ImageView imvfto = (ImageView) findViewById(R.id.imgFoto); 
       imvfto.setImageBitmap(bm); 
    } 

    if(requestCode == SELECT_FOTO) 
    { 
     imgfto.setImageBitmap(BitmapFactory.decodeFile(fnombre)); 
     new MediaScannerConnectionClient() { 
     private MediaScannerConnection msc = null; { 
       msc = new 
          MediaScannerConnection(getApplicationContext(), this); 
       msc.connect(); 
      } 
      public void onMediaScannerConnected() { 
       msc.scanFile(fnombre, null); 
      } 
      public void onScanCompleted(String path, Uri uri) { 
       msc.disconnect(); 
      } 
     };  
    } 
} 

我也嘗試另一種方式,但結果是一樣的。另一種方式是:

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 


    if(requestCode == CAPTURA_FOTO) 
    { 
     Uri imgselect = data.getData(); 
        InputStream is; 
     try 
     { 
      is = getContentResolver().openInputStream(imgselect); 
      BufferedInputStream bis = new BufferedInputStream(is); 
      Bitmap bmap = BitmapFactory.decodeStream(bis); 
      ImageView imvfto = (ImageView)findViewById(R.id.imgFoto); 
      imvfto.setImageBitmap(bmap); 
     } 
     catch (FileNotFoundException e) 
     { 
      Toast.makeText(Insertarlugar.this, "Error al cargar la 
          imagen", Toast.LENGTH_SHORT).show(); 
     } 
       } 

如何從畫廊中選擇圖像?

謝謝。

+0

你試過直接使用http://developer.android.com/reference/android/widget/ImageView.html#setImageURI(android.net.Uri)嗎?什麼是URI值? – buzeeg

+0

是的,我將setImageBitmap(bm)更改爲setImageUri(imgselect),我可以從圖庫中加載圖像。我現在工作的問題是將照片調整到imageview中。謝謝回答。 – axmug

+0

我已經添加了一些關於調整的信息。 – buzeeg

回答

0

也許你應該檢查中挑選圖片活動的結果是可以接受的,又名RESULT_OK:

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    //super.onActivityResult(requestCode, resultCode, data); 
    if (resultCode == RESULT_OK && requestCode == SELECT_FOTO) { 
     Uri selectedImage = data.getData(); 
     String[] filePathColumn = {MediaStore.Images.Media.DATA}; 

     Cursor cursor = getContentResolver().query(
       selectedImage, filePathColumn, null, null, null); 
     if (cursor != null) { 
      cursor.moveToFirst(); 

      int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
      String filePath = cursor.getString(columnIndex); 
      cursor.close(); 

      Bitmap bitmap = getImageFromPath(filePath, 512, 512); 
     } 
    } 
} 

這裏是我用解碼我的位圖的方法,你把你的位圖所需的尺寸,以便你不要浪費不必要的內存:

private int calculateInSampleSize(
     BitmapFactory.Options options, int reqWidth, int reqHeight) { 
    // Raw height and width of image 
    final int height = options.outHeight; 
    final int width = options.outWidth; 
    int inSampleSize = 1; 

    if (height > reqHeight || width > reqWidth) { 

     // Calculate ratios of height and width to requested height and width 
     final int heightRatio = Math.round((float) height/(float) reqHeight); 
     final int widthRatio = Math.round((float) width/(float) reqWidth); 

     // Choose the smallest ratio as inSampleSize value, this will guarantee 
     // a final image with both dimensions larger than or equal to the 
     // requested height and width. 
     inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio; 
    } 

    return inSampleSize; 
} 

private Bitmap getImageFromPath(String filePath,int reqWidth, int reqHeight) { 
    BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inJustDecodeBounds = true; 
    BitmapFactory.decodeFile(filePath, options); 
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); 
    options.inJustDecodeBounds = false; 
    return BitmapFactory.decodeFile(filePath, options); 
}