使用图像选择器库加载图像

load image with image-chooser-library

我尝试使用这个库 https://github.com/coomar2841/image-chooser-library 在我的应用程序上加载图像。

当我 select 一张图片时,我有这条消息 "could'nt process no such file"

我的代码:

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();
        }
    }

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

结果是

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

我认为问题是在哪里激发了我的形象。如何在我的应用程序中加载我的图像?

那是一个UriUri 不是文件,因此您不能将它传递给 BitmapFactory 上的 decodeFile()。使用像 Picasso 或 Universal Image Loader 这样的图像加载库,或者拥有自己的后台线程,在 ContentResolver 上使用 openInputStream() 来读取 Uri 的内容,将流传递给decodeStream()BitmapFactory