使用image-chooser-library加载图像

时间:2015-06-09 17:12:49

标签: android android-image

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

当我选择图片时,我收到此消息"无法处理此类文件"

我的代码:

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}

我认为问题在于引发我的形象。我如何在我的应用程序中加载我的图像?

1 个答案:

答案 0 :(得分:0)

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