DIỄN ĐÀN TIN HOC
Bạn có muốn phản ứng với tin nhắn này? Vui lòng đăng ký diễn đàn trong một vài cú nhấp chuột hoặc đăng nhập để tiếp tục.
DIỄN ĐÀN TIN HOC

BÀI VIẾT MỚINGƯỜI GỬI CUỐI

      Thảo luận nhiều nhất

      Lượt xem nhiều nhất


      You are not connected. Please login or register

      [Android Tutorial] Save Image to Sdcard

         

      nhuvannhu

      #1[Android Tutorial] Save Image to Sdcard Empty Wed Jun 18, 2014 9:32 am

      nhuvannhu
      nhuvannhu Quản lý viên

      Giới tính : Nam

      Tổng số bài gửi Tổng số bài gửi : 18

      Points Points : 57

      Like Like : 3

      status : Không có gì để nói

      Project demo : [You must be registered and logged in to see this link.]
      [Android Tutorial] Save Image to Sdcard Kn3Tm6L
      Chú ý :Trong manifest.xml
      <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
      <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
      DemoSaveImage.java
      Code:

      import java.io.File;
      import java.io.FileNotFoundException;
      import java.io.FileOutputStream;
      import java.io.IOException;

      import android.os.Bundle;
      import android.os.Environment;
      import android.app.Activity;
      import android.graphics.Bitmap;
      import android.graphics.drawable.BitmapDrawable;
      import android.view.Menu;
      import android.view.View;
      import android.view.View.OnClickListener;
      import android.widget.Button;
      import android.widget.ImageView;

      public class DemoSaveImage extends Activity {
         ImageView imageView;
         Button btSave ;
         @Override
         protected void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_demo_save_image);
            imageView = (ImageView)findViewById(R.id.imageView1);
            btSave  = (Button)findViewById(R.id.btSave);
            imageView.setImageDrawable(getResources().getDrawable(R.drawable.ic_launcher));
            btSave.setOnClickListener(new OnClickListener() {
               
               @Override
               public void onClick(View arg0) {
               
                  // TODO Auto-generated method stub
                  // TODO Auto-generated method stub
                  BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
                   Bitmap bitmap = drawable.getBitmap();
                   File filepath = Environment.getExternalStorageDirectory();

                  // Create a new folder in SD Card
                  File dir = new File(filepath.getAbsolutePath());
                  dir.mkdirs();
                  File file = new File(dir, "demosave.png");
                   boolean success = false;

                   // Encode the file as a PNG image.
                   FileOutputStream outStream;
                   try {

                       outStream = new FileOutputStream(file);
                       bitmap.compress(Bitmap.CompressFormat.PNG, 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();
                   }
               }
            });
         }

         @Override
         public boolean onCreateOptionsMenu(Menu menu) {

            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.demo_save_image, menu);
            return true;
         }

      }
      activity_demo_save_image.xml
      Code:
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
         android:orientation="vertical"
         android:layout_gravity="center"
          tools:context=".DemoSaveImage" >

          <ImageView
              android:id="@+id/imageView1"
              android:layout_width="200dp"
              android:layout_height="200dp"
              android:layout_alignParentBottom="true"
              android:layout_centerHorizontal="true"
              android:layout_marginBottom="68dp"
              android:src="@drawable/ic_launcher" />

          <Button
              android:id="@+id/btSave"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_above="@+id/imageView1"
              android:layout_alignLeft="@+id/imageView1"
              android:layout_marginBottom="59dp"
              android:layout_marginLeft="45dp"
              android:text="Save" />

      </LinearLayout>
         
            

      [Trang 1 trong tổng số 1 trang]

      Thông tin chuyên mục

      Bạn không có quyền trả lời bài viết