Milan Gajera
Milan Gajera

Reputation: 970

How can i copy image from drawable folder and paste into files directory in android?

I want to copy image from drawable and paste into files directory in android? Note:files directory lie in DDMS file explorer

I have lot of images in drawalbe which is set into gridview and i set one button over every images when i click on particular button it's copy and paste into files directory.

Is it possible?

This is my adapter class

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(){

    }
}

Please, anyone who help me to solve this task.

Upvotes: 1

Views: 352

Answers (1)

Rohit Patil
Rohit Patil

Reputation: 1068

Replace

fos.write(position)

With

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

Also add write external permission to manifest.xml

Upvotes: 2

Related Questions