如何从viewPager中保存图库中的图像和textview文本。它只保存图像

时间:2017-08-22 10:22:03

标签: android

 public Object instantiateItem(ViewGroup container, final int position) {
    layoutInflater=(LayoutInflater)cn.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View item_view= layoutInflater.inflate(R.layout.swipe_layout,container,false);


    final ImageView imageView=(ImageView)item_view.findViewById(R.id.sl_imageView);
    final TextView textView=(TextView)item_view.findViewById(R.id.tv_image);
    Button save=(Button)item_view.findViewById(R.id.btn_save);
    imageView.setImageResource(image_resource[position]);
    textView.setText("Image :"+position);
    final LinearLayout l_Layout=(LinearLayout)item_view.findViewById(R.id.LN_Layout);
    final String imageNm=textView.getText().toString();
    container.addView(l_Layout);

    save.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


            final Random generator = new Random();
            int n = 10000;
            n = generator.nextInt(n);
            final String fname = imageNm+ ".jpg";
            myDir.mkdirs();

            File image = new File(myDir, fname);


            Drawable drawable = cn.getResources().getDrawable(image_resource[position]);
            Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
            FileOutputStream outStream;

            try {

                outStream = new FileOutputStream(image);
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
                 /*100 to keep full quality of the image*/

                outStream.flush();
                outStream.close();
                success = true;
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            if (success) {
                Toast.makeText(cn,
                        "Image saved with success at /sdcard/temp_image",
                        Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(cn,
                        "Error during image saving", Toast.LENGTH_LONG)
                        .show();
            }

            cn.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://mnt/sdcard/" + Environment.getExternalStorageDirectory())));

        }

    });

0 个答案:

没有答案