需要从ScrollView中的图像获取路径

时间:2016-01-26 13:17:29

标签: android

我正在开发一款可以拍摄一些照片的应用,并将其存储在设备的SD中,并存储在给定的文件夹中。

稍后,该图像显示在ScrollView内的LinearLayout(水平)内。如果用户长按一下,我需要一种从SD文件夹中删除图像的方法。这是我的代码:

拍照后,在onActivityResult中,我称之为:

public void updatePhotos(){
    hScrollView.removeAllViews();
    LinearLayout lLayout=new LinearLayout(ctx);
    lLayout.setOrientation(LinearLayout.HORIZONTAL);
    hScrollView.addView(lLayout);
    File targetDirector = new File(targetPath);
    File[] files = targetDirector.listFiles();
    for (File file : files){
        lLayout.addView(insertPhoto(file.getAbsolutePath()));
    }
}

public View insertPhoto(String path){
    Bitmap bm = decodeSampledBitmapFromUri(path, 220, 220);

    LinearLayout layout = new LinearLayout(ctx);
    layout.setLayoutParams(new LinearLayout.LayoutParams(250, 250));
    layout.setGravity(Gravity.CENTER);

    ImageView imageView = new ImageView(ctx);
    imageView.setLayoutParams(new LinearLayout.LayoutParams(220, 220));
    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
    imageView.setImageBitmap(bm);
    imageView.setOnLongClickListener(new View.OnLongClickListener() {

        @Override
        public boolean onLongClick(View v) {

            //LongClick logic here


            return false;
        }
    });

    layout.addView(imageView);
    return layout;
}

如何实现我的目标?谢谢。

1 个答案:

答案 0 :(得分:0)

您可以在这种情况下使用标签:

 public View insertPhoto(String path){
        Bitmap bm = decodeSampledBitmapFromUri(path, 220, 220);

        LinearLayout layout = new LinearLayout(ctx);
        layout.setLayoutParams(new LinearLayout.LayoutParams(250, 250));
        layout.setGravity(Gravity.CENTER);

        ImageView imageView = new ImageView(ctx);
        imageView.setLayoutParams(new LinearLayout.LayoutParams(220, 220));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setImageBitmap(bm);
        imageView.setTag(path);
        imageView.setOnLongClickListener(new View.OnLongClickListener() {

            @Override
            public boolean onLongClick(View v) {

                //LongClick logic here
                  System.out.println(v.getTag())
                  File file= new File(android.os.Environment.getExternalStorageDirectory()
                  + v.getTag());
                  if(file.exists()) file.delete();

                return false;
            }
        });

解决您的问题?