如何从drawable文件夹中复制图像并粘贴到android中的files目录中?

时间:2015-12-12 10:22:44

标签: android

我想将图像从drawable复制并粘贴到android中的files目录中? 注意:files目录位于DDMS文件资源管理器

我在drawalbe中有很多图像设置为gridview,当我点击特定按钮时,我在每个图像上设置了一个按钮,它复制并粘贴到文件目录中。

有可能吗?

这是我的适配器类

public class ImageAdapter extends BaseAdapter {

    String[] name;
    Context context;
    int[] imageId;

    private static LayoutInflater inflater = null;

    public ImageAdapter(Context context,String[] name, int[] imageId) {
        this.name=name;
        this.context = context;
        this.imageId = imageId;
        inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return this.name.length;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

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

    public class Holder {
        ImageButton btnDownload;
        ImageView img;
    }
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        Holder holder = new Holder();
        final View rowView;

        rowView = inflater.inflate(R.layout.custome_image, null);
        holder.btnDownload = (ImageButton) rowView.findViewById(R.id.btndownload);
        holder.img = (ImageView) rowView.findViewById(R.id.imageView1);
        holder.img.setAdjustViewBounds(true);
        final Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), imageId[position]);
        int height = (bitmap.getHeight() * 512 / bitmap.getWidth());
        Bitmap scale = Bitmap.createScaledBitmap(bitmap, 512, height, true);
        holder.img.setImageBitmap(scale);
        holder.btnDownload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    File root = new File(context.getFilesDir(), "IMAGES");

                    if (!root.exists()) {
                        root.mkdirs();
                    }

                    File file=new File(root+File.separator+position+".jpeg");
                    Log.e("Path", "" + file);
                    file.createNewFile();

                    FileOutputStream fos = new FileOutputStream(file);
                    fos.write(position);
                    fos.close();
                }
                catch (Exception e){
                    e.printStackTrace();
                }
            }
        });

        holder.img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(rowView.getContext(), FullImaeActivity.class);
                intent.putExtra("id", position);
                //Uri path = Uri.parse("android.resource://com.domore.Angelnx/" + position);

                String imgpath=path.toString();
                Log.e("ImagePath",imgpath);
                rowView.getContext().startActivity(intent);
            }
        });
        return rowView;
    }
    public void moveImage(){

    }
}

请帮助我解决此任务的任何人。

1 个答案:

答案 0 :(得分:1)

替换

fos.write(position)

使用

bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos); 
fos.flush(); 

还为manifest.xml添加写入外部权限