我无法将我的图像带到从SD卡加载的新活动

时间:2017-10-17 14:47:57

标签: android image gridview android-gallery

图片活动

public class ImageAdapter extends BaseAdapter {


private Context context;
Bitmap bm;

ArrayList<String>images = new ArrayList<String>();



//  Integer[] images;
void add(String path) {
    images.add(path);
}


public ImageAdapter(Context c) {
    context = c;
}

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

@Override
public Object getItem(int position) {

    return images;
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    ImageView imageView = new ImageView(context);
    if (convertView == null) {
        //     imageView.setImageResource(images[position]);
        //   imageView.setImageBitmap(images);
        imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        imageView.setLayoutParams(new GridView.LayoutParams(240, 240));
        return imageView;
    } else {
        imageView = (ImageView) convertView;

    }
     bm = decodeSampledBitmapFromUri(images.get(position), 220, 220);
    imageView.setImageBitmap(bm);
return imageView;
}

private Bitmap decodeSampledBitmapFromUri(String path, int reqWidth, int reqHeight) {
    Bitmap bm = null;
    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(path, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    bm = BitmapFactory.decodeFile(path, options);

    return bm;
}
public int calculateInSampleSize(

        BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {
        if (width > height) {
            inSampleSize = Math.round((float)height / (float)reqHeight);
        } else {
            inSampleSize = Math.round((float)width / (float)reqWidth);
        }
    }

    return inSampleSize;
}
}

主要活动

gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent i= new Intent(getApplicationContext(), FullImageActivity.class);
            i.putExtra("id", position);
            startActivity(i);

        }
    });

FullImageActivity

  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_full_image);

    Intent i=getIntent();

    int position= i.getExtras().getInt("id");
    ImageAdapter imageAdapter= new ImageAdapter(this);





    ImageView imageView=(ImageView)findViewById(R.id.imageView);

     imageView.setImageBitmap(imageAdapter.bm); \\here I have to show my images that are read from Storage


}

我无法正确传递它,因为我的arraylist是字符串。我尝试了许多我知道的方法,但这些方法都不正常。我也试过互联网,但没有工作。 我无法通过fullimage活动显示我的图像。我从SD卡读取图像所以它没有在全视图中显示。提前致谢

1 个答案:

答案 0 :(得分:0)

您的代码未正确执行,因为它未正确设置。 首先,考虑使用Picasso或Glide或UniversalImageLoader将图像加载到占位符中,以避免在图像中滚动笨重的用户体验。

其次,将位置传递给第二个活动不是你的目标。您正试图传递图像。所以传递图像lol,

First on your getItem() 

你应该根据位置images.get(位置)

返回单个图像

而不是返回图像。

其次在您的点击处理程序

将其作为额外的传递

intent.addExtra(myAdapter.getItem(position))

或者如果你已经有了列表

intent.addExtra(myList.get(position))

or simply cheat with ActivityTwo.imageToShow = myList.get(position)
startActivityTwo

其中任何一个都可以正常工作。