2014-11-13 4 views
0

즉, 이미지를 가져 와서 업로드 할 수 있어야하는 앱이 있습니다. 업로드하기 전에 화면에 표시하고 싶습니다. 이상적으로는 갤러리를 사용하여 휴대 전화의 저장 공간에서 이미지를로드하거나 사진을 찍어 직접 업로드 할 수 있어야합니다.안드로이드 : 갤러리를 통해로드 된 카메라 이미지가 ImageView에 표시되지 않음

사진을 찍어서 문제없이 ImageView에 표시 할 수 있습니다. 갤러리에서 이미지를로드 할 수 있지만 일부 외부 소스에서 다운로드 한 이미지 만 ImageView에 표시됩니다. 예를 들어, 지난 주에 카메라로 사진을 찍었고 갤러리와 함께 사진을 선택하고 싶다면 사진이로드되지 않습니다. ImageView는 오류없이 그냥 비어 있습니다. 이것은 카메라로 촬영 한 모든 단일 이미지의 경우입니다. 갤러리를 사용하여로드하려고하면 작동하지 않지만 갤러리를 사용하여 다른 이미지를로드하면 작동합니다. 이것이 왜 그런지 이해할 수 없으므로 관련 코드를 제시하고 누군가 나를 도와 줄 수 있기를 바랍니다.

Intent pickPhoto = new Intent(Intent.ACTION_PICK, 
        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
startActivityForResult(pickPhoto , 1); 

그리고 그것은 이미지를로드하여 표시하려고 시도하는 것 onActivityResult를 내 코드 :이 당신을 도울 수

  Uri selectedImage = data.getData(); 
      String[] filePathColumn = { MediaStore.Images.Media.DATA }; 

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

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

      Bitmap imageBitmap = (Bitmap) BitmapFactory.decodeFile(picturePath); 

      imageview.setImageBitmap(imageBitmap); 

답변

0
private void chooseImageFromGalery() { 
    Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
    intent.setType("image/*"); 
    startActivityForResult(Intent.createChooser(intent, "Choose Image"), 101); 
} 

if (requestCode == 101 && resultCode == Activity.RESULT_OK) { 
     Uri selectedImageUri = data.getData(); 
     try { 
      photoFile = createImageFile(); 
     } catch (IOException e1) { 
      e1.printStackTrace(); 
     } 
     try { 
      copyFile(new File(getRealPathFromURI(data.getData())), photoFile); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     refreshFragmentData(); 
    } 
private void copyFile(File sourceFile, File destFile) throws IOException { 
    if (!sourceFile.exists()) { 
     return; 
    } 

    FileChannel source = null; 
    FileChannel destination = null; 
    source = new FileInputStream(sourceFile).getChannel(); 
    destination = new FileOutputStream(destFile).getChannel(); 
    if (destination != null && source != null) { 
     destination.transferFrom(source, 0, source.size()); 
    } 
    if (source != null) { 
     source.close(); 
    } 
    if (destination != null) { 
     destination.close(); 
    } 

} 

private String getRealPathFromURI(Uri contentUri) { 
    String[] proj = { MediaStore.Video.Media.DATA }; 
    Cursor cursor = getActivity().managedQuery(contentUri, proj, null, null, null); 
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
    cursor.moveToFirst(); 
    return cursor.getString(column_index); 
} 

private File createImageFile() throws IOException { 
    // Create an image file name 
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 
    String imageFileName = "JPEG_" + timeStamp + "_"; 
    File storageDir = new File(CommonParams.MASTER_STORAGE_PATH + "/" + CommonParams.categorySelected); 
    File image = File.createTempFile(imageFileName, /* prefix */ 
      ".jpg", /* suffix */ 
      storageDir /* directory */ 
    ); 

    // Save a file: path for use with ACTION_VIEW intents 
    mCurrentPhotoPath = "file:" + image.getAbsolutePath(); 
    return image; 
} 

희망.

0
   Intent pickPhoto = new Intent(
           Intent.ACTION_PICK, 
           android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
         pickPhoto.setType("image/*"); 
         startActivityForResult(pickPhoto, 1); 

그리고 그것은 이미지를로드하여 표시하려고 시도하는 것 onActivityResult를 내 코드 :

   Intent imageReturnedIntent; 
      Uri selectedImage = data.getData(); 
      String[] filePathColumn = { MediaColumns.DATA }; 
      Cursor cursor = getContentResolver().query(selectedImage, 
        filePathColumn, null, null, null); 
      cursor.moveToFirst(); 
      int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
      String filePath = cursor.getString(columnIndex); 
      File mFile = new File(filePath); 
      cursor.close(); 
      BitmapFactory.Options options = new BitmapFactory.Options(); 
      options.inSampleSize = 2; 
      Bitmap bp = BitmapFactory.decodeFile(filePath, options); 
관련 문제