2017-03-09 4 views
2
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_view_image); 

    imageView = (ImageView) findViewById(R.id.imageView); 

    Intent intent = getIntent(); 
    Uri imageUri = intent.getData(); 
    // Picasso.with(this).load(imageUri).into(imageView); 

    if(imageUri == null){ 
     Log.d(TAG, "Check"); 
    } 






     //BitmapFactory.Options options = new BitmapFactory.Options(); 
    // options.inSampleSize = 8; 
    // bm = MediaStore.Images.Media.getBitmap(getContentResolver(),imageUri); 

    Bitmap bm = null; 

    try{ 
     InputStream image; 
    try{ 
     image = getContentResolver().openInputStream(imageUri); 
     bm = BitmapFactory.decodeStream(image); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } 
    } 
    catch (Exception e){ 
     e.printStackTrace(); 
    } 




    //BitmapFactory.Options options = new BitmapFactory.Options(); 
    // bm = BitmapFactory.decodeFile(file.getAbsolutePath(),options); 


    if ((bm == null)) { 
     prints("It doesn't work"); 
    } 

    //Log.d(TAG,"Going to convert image."); 
     imageView.setImageBitmap(bm); 

J'ai regardé un tas de questions StackOverFlow, je les ai toutes essayées comme vous pouvez le voir dans le code commenté. J'essaie d'obtenir un bitmap qui est inchangé lorsque la photo est prise. La position des pixels de l'image bitmap est cruciale pour mon objectif. L'image est de haute qualité et peut-être un fichier plus volumineux. Mon Bitmap est toujours nul, je me suis assuré que mon Uri ne l'est pas. Des idées?Obtenir un bitmap à partir d'un Uri

+1

'image InputStream;'. S'il vous plaît ne pas écrire de code illisible. Ne nommez pas un flux d'entrée une image. – greenapps

Répondre

0

Le fichier a une trop grande résolution pour en faire un bitmap. Il n'y a pas assez de mémoire pour construire un bitmap et donc une valeur nulle est retournée. Essayez avec un très petit fichier image pour voir.

0

Vous pouvez obtenir directement un bitmap à partir d'un URI s'il provient de votre stockage mobile sous le code ci-dessous. Si le fichier est trop volumineux pour être chargé, vous devez compresser l'image et l'afficher. Pour cela, vous devez également utiliser la tâche asynchrone.

J'ai fait une tâche de compression d'image, vous pouvez l'utiliser pour une grande image

public class ConvertBase64Task extends AsyncTask<Void, Bitmap, Bitmap> { 

    private ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
    private File file; 

    private int CompressionRatio = 80; //You can change it by what ever ratio you want. in 0 to 100. 

    private boolean shouldrotate = true; 





    private ImageCompressiorListner imageCompressiorListner; 

    public ConvertBase64Task(File file) { 
     this.file = file; 
    } 

    public void setRotation(boolean isRotate) { 
     shouldrotate = isRotate; 
    } 


    public void setCompressionRatio(int Ratio) { 
     CompressionRatio = Ratio; 
    } 


    public void setImageCompressiorListner(ImageCompressiorListner imageCompressiorListner) { 
     this.imageCompressiorListner = imageCompressiorListner; 
    } 

    @Override 
    protected void onPreExecute(){ 
    } 

    @Override 
    protected Bitmap doInBackground(Void... params) { 



      try { 
       //***** Fatching file 
       //*****Code for Orientation 
       Matrix matrix = new Matrix(); 

       if (shouldrotate) { 
        ExifInterface exif1 = new ExifInterface(file.getAbsolutePath()); 
        int orientation = exif1.getAttributeInt(
          ExifInterface.TAG_ORIENTATION, 1); 
        Log.d("EXIF", "Exif: " + orientation); 
        if (orientation == 6) { 
         matrix.postRotate(90); 
        } else if (orientation == 3) { 
         matrix.postRotate(180); 
        } else if (orientation == 8) { 
         matrix.postRotate(270); 
        } else { 
         matrix.postRotate(0); 
        } 
       } else { 
        matrix.postRotate(0); 
       } 

       try { 

        BitmapFactory.Options option = new BitmapFactory.Options(); 
        option.inJustDecodeBounds = true; 
        BitmapFactory.decodeFile(file.getAbsolutePath(), option); 

        int file_size = Integer.parseInt(String.valueOf(file.length()/1024)); 
        Log.e("ImageSize", "" + file_size); 


        int scale = 1; 

        if (file_size < 512) { 
         Log.e("image size is good", "image size is less"); 
        } else if (file_size < 1024) { 
         Log.e("image size is 1 mb", "image size is heavy"); 
         scale = 2; 
        } else if (file_size < 1536) { 
         Log.e("image size is 1.5 mb", "image size is heavy"); 
         scale = 2; 
        } else if (file_size < 2048) { 
         Log.e("image size is 2 mb", "image size is heavy"); 
         scale = 4; 
        } else { 
         Log.e("image size > 2 mb", "image size is heavy"); 
         scale = 4; 
        } 

        Log.e("Scale", "Finaly Scaling with " + scale); 

        BitmapFactory.Options o2 = new BitmapFactory.Options(); 
        o2.inSampleSize = scale; 
        Bitmap pickimg = BitmapFactory.decodeFile(file.getAbsolutePath(), o2); 

        if (pickimg.getWidth() > 1280 || pickimg.getHeight() > 1000) { 

         int width = pickimg.getWidth(); 
         int height = pickimg.getHeight(); 

         while (width > 1280 || height > 700) { 
          width = (width * 90)/100; 
          height = (height * 90)/100; 
         } 

         pickimg = Bitmap.createScaledBitmap(pickimg, width, height, true); 
        } else { 
         pickimg = Bitmap.createBitmap(pickimg, 0, 0, pickimg.getWidth(), pickimg.getHeight(), matrix, true); // rotating bitmap 
        } 

        pickimg.compress(Bitmap.CompressFormat.JPEG, CompressionRatio, baos); 
        return pickimg; 

       } catch (OutOfMemoryError e) { 
        e.printStackTrace(); 
        return null; 
       } catch (Exception e) { 
        e.printStackTrace(); 
        return null; 

       } 

      } catch (Throwable e) { 
       e.printStackTrace(); 
       return null; 
      } 

    } 

    @Override 
    protected void onPostExecute(Bitmap result) { 
     super.onPostExecute(result); 
     if (result != null) { 
      if(imageCompressiorListner!=null){ 
       imageCompressiorListner.onImageCompressed(result); 
      } 
     } else { 
      if(imageCompressiorListner!=null){ 
       imageCompressiorListner.onError(); 
      } 
     } 
    } 


    public interface ImageCompressiorListner { 
     void onImageCompressed(Bitmap bitmap); 
     void onError(); 
    } 
} 

Vous pouvez l'utiliser comme ci-dessous

ConvertBase64Task task = new ConvertBase64Task(File Object of Image); 
     task.setImageCompressiorListner(new ConvertBase64Task.ImageCompressiorListner() { 
     @Override 
     public void onImageCompressed(Bitmap bitmap) { 

     } 

     @Override 
     public void onError() { 

     } 
    }); 
    task.execute(); 

Espérons que cela vous aider.

+0

Down Voters, Puis-je savoir ce qui ne va pas avec le code? –

2

Je pense que le code ci-dessous vous aidera à

private void setImage(String path, CircleImageView img) { 

    options = new DisplayImageOptions.Builder() 
      .cacheInMemory(true) 
      .cacheOnDisk(true) 
      .considerExifParams(true) 
      .bitmapConfig(Bitmap.Config.RGB_565) 
      .showImageOnLoading(R.drawable.user) 
      .showImageForEmptyUri(R.drawable.user) 
      .showImageOnFail(R.drawable.user) 
      .build(); 

    ImageLoader.getInstance().displayImage(path, img, options, new SimpleImageLoadingListener() { 

     @Override 
     public void onLoadingStarted(String imageUri, View view) { 
      super.onLoadingStarted(imageUri, view); 
     } 

     @Override 
     public void onLoadingFailed(String imageUri, View view, FailReason failReason) { 
      super.onLoadingFailed(imageUri, view, failReason); 
     } 

     @Override 
     public void onLoadingCancelled(String imageUri, View view) { 
      super.onLoadingCancelled(imageUri, view); 
     } 

     @Override 
     public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) { 
      super.onLoadingComplete(imageUri, view, loadedImage); 
     } 
    }, new ImageLoadingProgressListener() { 
     @Override 
     public void onProgressUpdate(String s, View view, int i, int i1) { 

     } 
    }); 
} 

utilisation chargeur d'image universelle pour SimpleImageLoadingListener()