doInBackground (Agenda. .. params) does not return bitmap image in listView

I'm 3 days ago now looking for an answer and find no satisfying. I created a listview to populate it with a picture and a text, the way the image is saved in sqlite database and image is in sd card of the phone, but only that not all rows from the database has a path to image so I'm placing a default image, but the problem is that even the images that are the path, not returning to this image, follows my adapter code below:

public class AgendaListAdapter extends ArrayAdapter<Agenda> {

private LayoutInflater inflater;
private List<Agenda> lista;
private Agenda agenda;
int resources;

//constructor
public AgendaListAdapter(Context context, int resource, List<Agenda> lista){
    super(context, resource, lista);

    this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    this.resources = resource;
    this.lista = lista;
}

@Override
public int getCount() {
    return lista.size();
}

@Override
public Agenda getItem(int position) {
    return lista.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    agenda = lista.get(position);
    //cria uma referencia para viewHolder
    //creates a reference to
    ViewHolder viewHolder;

    //verifica se a view esta sendo reusada ou não
    //verifies that the view is being reused or not
    if(convertView == null){
        viewHolder = new ViewHolder();

        convertView = inflater.inflate(R.layout.linha_listar_telefones, null);
        viewHolder.mNome = (TextView) convertView.findViewById(R.id.nome);
        viewHolder.mImagem = (ImageView) convertView.findViewById(R.id.listar_contato_imagem);
        convertView.setTag(viewHolder);
    }else{

        viewHolder = (ViewHolder) convertView.getTag();
    }



    if(agenda != null){
        if(viewHolder.mNome != null){
            viewHolder.mNome.setText(agenda.nome);              
        }

        if(viewHolder.mImagem != null){
            viewHolder.mImagem.setTag(lista.get(position));
            Log.w("Livro", "Positiom: "+position);
                Log.e("Livro", "Viewhol.imagem não é nulo "+viewHolder.mImagem);
                new loadImageTask(viewHolder.mImagem).execute();
        }

    }


    return convertView;
}




private static class ViewHolder{

    protected TextView mNome;
    protected ImageView mImagem;
}
Bitmap b = null;

private class loadImageTask extends AsyncTask<Agenda, Void, Bitmap>{

    private ImageView imv;
    private String path;

    public loadImageTask(ImageView imv) {
        this.imv = imv;
        this.path = agenda.caminho_imagem;
   }



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

        Log.w("Livro", "path: "+path);

        File file = new File( 
                Environment.getExternalStorageDirectory().getAbsolutePath() + path);

        if(file.exists()){
            b =  BitmapFactory.decodeFile(file.getAbsolutePath());

        }

        return b;

        }

    @Override
    protected void onPostExecute(Bitmap result) {
        if(result != null && imv != null){
            Log.i("Livro", "Result do onPostExcute não é nulo: "+result);
            imv.setVisibility(View.VISIBLE);
            imv.setImageBitmap(result);
        }else{
            Log.i("Livro", "Result do onPostExcute é nulo: "+result);
            imv.setImageResource(R.drawable.foto_pessoa);
        }
    }


}

}

In the logcat path returning this: content ://media/external/images/media/16 The variable file is returning the following result: / storage/emulated/sdcard0content ://media/external/images/media/16

I know the result of this wrong file, but would transform into uri path and pass a uri to file?

Thanks guys!!!

Answers


The path //media/external/images/media/16 is a Media Uri...You have to get the actual file path from the Uri and then set the image to the ImageView.

Get the real file path from Media Uri as below and the returned path is absolute file path...

public String getRealPathFromURI(Context context, Uri contentUri) {
  Cursor cursor = null;
  try { 
    String[] proj = { MediaStore.Images.Media.DATA };
    cursor = context.getContentResolver().query(contentUri,  proj, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
  } finally {
    if (cursor != null) {
      cursor.close();
    }
  }
}

Get Bitmap from the path returned by getRealPathFromURI() method as below...

Context mContext;

public loadImageTask(Context context, ImageView imv) {

    this.mContext = context;

    this.imv = imv;
    this.path = agenda.caminho_imagem;
}

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

    Log.w("Livro", "path: "+path);

    File file = new File(getRealPathFromURI(mContext, path));

    if(file.exists()){
        b =  BitmapFactory.decodeFile(file.getAbsolutePath());

    }

    return b;
}

And pass the context of the activity through the asynctask constructor as below...

new loadImageTask(context, viewHolder.mImagem).execute();

Need Your Help

How do you restart a Java applet?

java applet awt

I'm making a game for a school project and have been stuck on this part for a few days. How would I go about restarting the following applet? I have tried to make a restart() method, but lack the

Get Traceback of warnings

python warnings traceback

In numpy we can do np.seterr(invalid='raise') to get a traceback for warnings raising an error instead (see this post).