2014-09-12 7 views
0

На картинке HTC в ImageView не отображается. Я пытаюсь загрузить изображение из файла:Загрузить изображение из файла на HTC

Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
       i.setType("image/jpeg"); 
       startActivityForResult(i, 0); 

Accept результат:

public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (resultCode == Activity.RESULT_OK) { 
      { 
       if (data != null) { 
        if (requestCode == 0){ 
         Uri selectedImage = data.getData(); 
         loadImageToImageView(selectedImage); 
         } 
        } 
      } 
     } 
    } 

делать в фоновом режиме в loadImageToImageView (Uri Uri):

protected Bitmap doInBackground(String... file) { 
    InputStream in = null; 
     Bitmap bitmap = null; 
     try { 
      in = getContentResolver().openInputStream(uri); 
      bitmap = BitmapFactory.decodeStream(in); 
      new WeakReference<Bitmap>(bitmap); 
     } catch (Exception e) { 
      //.... 
     } finally { 
      try { 
       if (in != null) { 
        in.close(); 
       } 
      } catch (IOException ioex) { 
       //.... 
      } 
     } 
return bitmap; 
} 


protected void onPostExecute(Bitmap result) { 
     if (result != null) { 
      imageView.setImageBitmap(result); 
     } 
    } 

В HTS ONE Фотография X не отображается. В чем проблема? На других устройствах все работает нормально. URI не NULL, нет ошибок

+0

Возможно, вы получаете исключение. Что появляется в LogCat? В конце концов, ваш '// ....' наверняка использует 'Log.e()' для регистрации этих исключений в LogCat. – CommonsWare

+0

У меня нет ошибок. Только мое изображение не отображается на HTC. ImageView, как будто пустой – user3815165

+0

проверить разрешение изображения .. очень важно ... – shashi2459

ответ

0

Попробуйте этот путь, надейтесь, что это поможет вам решить вашу проблему.

Intent intent = new Intent(); 
intent.setType("image/*"); 
intent.setAction(Intent.ACTION_GET_CONTENT); 
startActivityForResult(Intent.createChooser(intent, ""), 0); 

public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (resultCode == Activity.RESULT_OK) { 
     if (data != null) { 
      if (requestCode == 0){ 
       imageView.setImageBitmap(decodeFile(getAbsolutePath(data.getData()))); 
      } 
     } 
    } 
} 

public String getAbsolutePath(Uri uri) { 
    if(Build.VERSION.SDK_INT >= 19){ 
     String id = uri.getLastPathSegment().split(":")[1]; 
     final String[] imageColumns = {MediaStore.Images.Media.DATA }; 
     final String imageOrderBy = null; 
     Uri tempUri = getUri(); 
     Cursor imageCursor = getContentResolver().query(tempUri, imageColumns,MediaStore.Images.Media._ID + "="+id, null, imageOrderBy); 
     if (imageCursor.moveToFirst()) { 
      return imageCursor.getString(imageCursor.getColumnIndex(MediaStore.Images.Media.DATA)); 
     }else{ 
      return null; 
     } 
    }else{ 
     String[] projection = { MediaStore.MediaColumns.DATA }; 
     Cursor cursor = getContentResolver().query(uri, projection, null, null, null); 
     if (cursor != null) { 
      int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA); 
      cursor.moveToFirst(); 
      return cursor.getString(column_index); 
     } else{ 
      return null; 
     } 
    } 

private Uri getUri() { 
    String state = Environment.getExternalStorageState(); 
    if(!state.equalsIgnoreCase(Environment.MEDIA_MOUNTED)) 
    return MediaStore.Images.Media.INTERNAL_CONTENT_URI; 
    else 
    return MediaStore.Images.Media.EXTERNAL_CONTENT_URI; 
} 


public Bitmap decodeFile(String path) { 
    try { 
     BitmapFactory.Options o = new BitmapFactory.Options(); 
     o.inJustDecodeBounds = true; 
     BitmapFactory.decodeFile(path, 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 scale = 1; 
     while (o.outWidth/scale/2 >= REQUIRED_SIZE && o.outHeight/scale/2 >= REQUIRED_SIZE) 
      scale *= 2; 
     // Decode with inSampleSize 
     BitmapFactory.Options o2 = new BitmapFactory.Options(); 
     o2.inSampleSize = scale; 
     return BitmapFactory.decodeFile(path, o2); 
    } catch (Throwable e) { 
     e.printStackTrace(); 
    } 
    return null; 
} 
Смежные вопросы