load image with image-chooser-library

1.8k views Asked by At

I try to use this library https://github.com/coomar2841/image-chooser-library to load image on my application.

When I select a image, I have this message "could'nt process no such file"

My code:

public class EditListeModelActivity extends Activity implements ImageChooserListener{


EditText titre;
CheckBox isvisible;
ActionBar actionBar;
ImageView ProfileImage;
private static int RESULT_LOAD_IMAGE = 1;
private boolean imageModifie=false;
private Bitmap newImage;

Builder b;

private ProgressBar pbar;
private ImageChooserManager imageChooserManager;
private String filePath;
private int chooserType;
private String uriFile;
private Button btnValide;




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


    if(savedInstanceState!=null)
    {
        if (savedInstanceState.containsKey("uri_path")) {
            uriFile = savedInstanceState.getString("uri_path");
        }
    }

    actionBar = getActionBar();
    actionBar.setDisplayOptions( ActionBar.DISPLAY_SHOW_HOME|ActionBar.DISPLAY_SHOW_TITLE|ActionBar.DISPLAY_SHOW_CUSTOM);


    titre=(EditText)findViewById(R.id.nomEdit);
    isvisible=(CheckBox)findViewById(R.id.isvisible);
    btnValide=(Button)findViewById(R.id.EditListeModelNow);


    ProfileImage = (ImageView) findViewById(R.id.profileImageEdit);

    if(uriFile==null)
    {
    }
    else
    {
        ProfileImage.setImageURI(Uri.parse(uriFile));

        newImage=BitmapFactory.decodeFile(uriFile);
        ProfileImage.setImageBitmap(newImage);
        imageModifie=true;
    }


    b = new Builder(this);
    b.setTitle("Choisir photo");
    String[] types = {"Prendre une nouvelle photo", "Choisir une photo existante"};
    b.setItems(types, new OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {

            dialog.dismiss();
            switch(which){
            case 0:
                takePicture();
                break;
            case 1:
                chooseImage();
                break;
            }
        }

    });


    ProfileImage.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {

            b.show();
            //chooseImage();
          /*   
            Intent i = new Intent(
                    Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

            startActivityForResult(i, RESULT_LOAD_IMAGE);*/
        }
    });


    pbar = (ProgressBar) findViewById(R.id.progressBarEditProfile);
    pbar.setVisibility(View.GONE);
    ProfileImage.setVisibility(View.VISIBLE);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.edit_liste_model, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK
            && (requestCode == ChooserType.REQUEST_PICK_PICTURE || requestCode == ChooserType.REQUEST_CAPTURE_PICTURE)) {
        if (imageChooserManager == null) {
            reinitializeImageChooser();
        }
           Log.e("imageChooserManager data ",data.toString());
        imageChooserManager.submit(requestCode, data);
    } else {
        pbar.setVisibility(View.GONE);
        ProfileImage.setVisibility(View.VISIBLE);
    }
}

@Override
public void onImageChosen(final ChosenImage image) {
    runOnUiThread(new Runnable() {

        @Override
        public void run() {
            pbar.setVisibility(View.GONE);
            ProfileImage.setVisibility(View.VISIBLE);
            if (image != null) {


                File f=new File(image.getFileThumbnail());
                ProfileImage.setImageURI(Uri.parse(f.toString()));
                uriFile=f.toString();


        newImage=BitmapFactory.decodeFile(f.toString());
                ProfileImage.setImageBitmap(newImage);
                imageModifie=true;

            }
        }
    });
}

@Override
public void onError(final String reason) {
    runOnUiThread(new Runnable() {

        @Override
        public void run() {
            pbar.setVisibility(View.GONE);
            ProfileImage.setVisibility(View.VISIBLE);
            Toast.makeText(EditListeModelActivity.this, reason,
                    Toast.LENGTH_LONG).show();
        }
    });
}

// Should be called if for some reason the ImageChooserManager is null (Due
    // to destroying of activity for low memory situations)
    private void reinitializeImageChooser() {
        imageChooserManager = new ImageChooserManager(this, chooserType,
                "myfolder", true);
        imageChooserManager.setImageChooserListener(this);
        imageChooserManager.reinitialize(filePath);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("chooser_type", chooserType);
        outState.putString("media_path", filePath);
        outState.putString("uri_path", uriFile);

    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        if (savedInstanceState != null) {
            if (savedInstanceState.containsKey("chooser_type")) {
                chooserType = savedInstanceState.getInt("chooser_type");
            }

            if (savedInstanceState.containsKey("media_path")) {
                filePath = savedInstanceState.getString("media_path");
            }
            if (savedInstanceState.containsKey("uri_path")) {
                uriFile = savedInstanceState.getString("uri_path");
            }
        }
    }

    private void takePicture() {
        chooserType = ChooserType.REQUEST_CAPTURE_PICTURE;
        imageChooserManager = new ImageChooserManager(this,
                ChooserType.REQUEST_CAPTURE_PICTURE, "myfolder", true);
        imageChooserManager.setImageChooserListener(this);
        try {
            pbar.setVisibility(View.VISIBLE);
            ProfileImage.setVisibility(View.GONE);
            filePath = imageChooserManager.choose();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void chooseImage() {
        chooserType = ChooserType.REQUEST_PICK_PICTURE;
        imageChooserManager = new ImageChooserManager(this,
                ChooserType.REQUEST_PICK_PICTURE, "myfolder", true);
        imageChooserManager.setImageChooserListener(this);
        try {
            pbar.setVisibility(View.VISIBLE);
            ProfileImage.setVisibility(View.GONE);
            filePath = imageChooserManager.choose();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

At ligne `Log.e("imageChooserManager data ",data.toString());

the result is

Intent { dat=content://com.android.providers.media.documents/document/image:22501 flg=0x1 }

I think that the probleme is where is stoked my image. How I do to load my image in my application?

1

There are 1 answers

0
CommonsWare On BEST ANSWER

That is a Uri. A Uri is not a file, and so you cannot pass it to decodeFile() on BitmapFactory. Either use an image loading library like Picasso or Universal Image Loader, or have your own background thread that uses openInputStream() on a ContentResolver to read in the contents of that Uri, passing the stream to decodeStream() on BitmapFactory.