1

下面是代码。在这里,它捕获图像但未保存在指定文件夹中,而是以该名称创建一个空文件夹。该代码在捕获图像时运行良好,但不能保存到该文件夹​​中查看。因为我是 android 新手,不知道如何解决这个问题。提前感谢

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/ic_launcher"
    android:orientation="vertical" >

    <Button
        android:id="@+id/Button01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Capture" >
    </Button>

    <ImageView
        android:id="@+id/ImageView01"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone">
    </ImageView>

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>


    public class MainActivity extends Activity implements OnClickListener {

        LinearLayout view;
        Button myBtn;
        ImageView view2;
        ImageView i;
        LayoutInflater inflater;

        String[] items = { "Multiply 2 Numbers below 100",
                "Multiply 2 Numbers above 100", "Multiply Any 2 Digit Numbers",
                "Squaring a Number", "Easy Squaring a Number Which Ends With 1",
                "Easy Squaring a Number Which Ends With 5",
                "Easy Multiply Any number By 11",
                "Multiply 2 numbers by a condition",
                "Finding Square of an adjacent number: One above",
                "Finding Square of an adjacent number: One below",
                "Checking a Number divisible by 4 or not?",
                "Multiply any number by 12?", "gbbbbbbbbbb", "gbbbbbbbbbbbbb",
                "gbbbbbbbbbbbbb", "gbbbbbbbbbb", "gbbbbbbbbbb", "bggggggggg",
                "bggggggggggggg","Multiply 2 Numbers above 100", "Multiply Any 2",
                "Squaring a Number", "Easy Squaring a Number Which Ends With 1",
                "Easy Squaring a Number Which Ends With 5",
                "Easy Multiply Any number By 11",
                "Multiply 2 numbers by a condition",
                "Finding Square of an adjacent number: One above",
                "Finding Square of an adjacent number: One below",
                "Checking a Number divisible by 4 or not?",
                "Multiply any number by 12?", "gbbbbbbbbbb", "gbbbbbbbbbbbbb",
                "gbbbbbbbbbbbbb", "gbbbbbbbbbb", "gbbbbbbbbbb", "bggggggggg",
                "bggggggggggggg" };
        ListView l1;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            LayoutInflater inflater = (LayoutInflater) this
                    .getSystemService(LAYOUT_INFLATER_SERVICE);
            l1 = (ListView) findViewById(R.id.listView1);
            myBtn = (Button) findViewById(R.id.Button01);
            i = (ImageView) findViewById(R.id.ImageView01);

            ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                    MainActivity.this, android.R.layout.simple_list_item_1,
                    android.R.id.text1, items);
            l1.setAdapter(adapter);
            myBtn.setOnClickListener(this);

        }
        @Override
        public void onClick(View v) {

            // TODO Auto-generated method stub
            LayoutInflater inflater = (LayoutInflater) this
                    .getSystemService(LAYOUT_INFLATER_SERVICE);
            LinearLayout root = (LinearLayout) inflater.inflate(
                    R.layout.activity_main, null);
            root.setDrawingCacheEnabled(true);

            Bitmap bmp = getBitmapFromView(this.getWindow().findViewById(
                    R.id.listView1));
            l1.setVisibility(View.GONE);
            i.setImageBitmap(bmp);
            i.setVisibility(View.VISIBLE);

        }

        public Bitmap getBitmapFromView(View v) {
            v.setLayoutParams(new LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.MATCH_PARENT));

            ListAdapter adapter  = l1.getAdapter();
            int itemscount       = adapter.getCount();
            int allitemsheight   = 0;
            for (int i = 0; i < itemscount; i++)
            {



 View childView      = adapter.getView(i, null, l1);
                childView.measure(MeasureSpec.makeMeasureSpec(l1.getWidth(), MeasureSpec.EXACTLY),
                        MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));

                childView.layout(0, 0, childView.getMeasuredWidth(), childView.getMeasuredHeight());
                childView.setDrawingCacheEnabled(true);
                childView.buildDrawingCache();

                allitemsheight+=childView.getMeasuredHeight();


        }
            v.layout(0, 0, v.getMeasuredWidth(),allitemsheight);



            Bitmap b = Bitmap.createBitmap(v.getMeasuredWidth(),allitemsheight,
                    Bitmap.Config.ARGB_8888);

            Canvas c = new Canvas(b);
            v.draw(c);
           // return b;

            String root = Environment.getExternalStorageDirectory() + "/PhotoEmail";

             File file = new File(root); 

             file.mkdirs();

             File file1 = new File(root + "/" +"images"+ ".png");

             System.out.println(file1);

             try {
                FileOutputStream fos = new FileOutputStream(file1);
                 if (fos != null) {
                            b.compress(Bitmap.CompressFormat.PNG, 90, fos);
                            //System.out.println("fos");
                          fos.close();
                           }
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();

    }
            return b;
    }
    }
4

0 回答 0