简体   繁体   中英

I'm getting an error.( Realpath to Image file)

enter image description here

I'm first to use android studio, so I'm having many difficulties. I want to make my own gallery in GALLERY tab. Gallery tab is a fragment and gridview. I get URI and convert into path. That files are in simulator. Unintentionally, realpath is viewed in GALLERY TAB, and I want to look real image in tab. I don't know how to invert path into image files. Sorry for not being good in English.

int a = 0;
public String getPath(Uri uri) {
    String[] projection = {MediaStore.Images.Media.DATA};
    Cursor cursor = getActivity().getContentResolver().query(uri, null, null, null ,null);
    int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToPosition(a);
    return cursor.getString(columnIndex);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_tab2, container, false);
    GridView f1 = view.findViewById(R.id.gridView);


    Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
    Uri[] projection = {MediaStore.Images.Media.EXTERNAL_CONTENT_URI};

    Cursor cursor = getActivity().getContentResolver().query(uri, null, null, null ,null);
    ArrayList<String> result = new ArrayList<String>();

    if(cursor.moveToFirst()){
        do{
            result.add(getPath(uri));
            a++;
        }while(cursor.moveToNext());
    }


    ArrayAdapter<String> arrayadapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_list_item_1, result);
    f1.setAdapter(arrayadapter);


    return f1;
}

This code is in fragment file.

Add ImageView as item inside your adapter layout. Add Glide library to your project and use the uri to load the images. You can use other image loading library as well.

Check this link on how to use the library and sample. https://github.com/bumptech/glide

sample code

ImageView imageView = (ImageView) findViewById(R.id.my_image_view);
GlideApp.with(this).load(new File(pictureUri.getPath())).into(imageView);

You cannot use ArrayAdapter to achieive what you want. Search for RecyclerView with imageView you will get lots of sample code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM