android 查看图片、保存图片

配置photoview-library-1.2.2.jar 到libs

photoview-library-1.2.2.jar 下载地址  http://pan.baidu.com/s/1nviEKtj

MainActivity

 

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import uk.co.senab.photoview.PhotoView;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener{

    private PhotoView mPhotoView;
    private String img;
    private Bitmap bitmap;
    /**
     * 默认文件保存路径
     */
    private static final String SAVE_PIC_PATH = Environment.getExternalStorageState().equalsIgnoreCase(Environment.MEDIA_MOUNTED)
            ? Environment.getExternalStorageDirectory().getAbsolutePath() : "/mnt/sdcard";//保存到SD卡
    private static final String SAVE_REAL_PATH = SAVE_PIC_PATH + "/good/savePic";//保存的确切位置

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 1) {
                mPhotoView.setImageBitmap(bitmap);
            }
        }
    };

    private void init() {
        mPhotoView = (PhotoView) findViewById(R.id.iv_photo);
//        img = getIntent().getStringExtra("image");
        img = "http://p1.pstatp.com/origin/9e000062f3b52ea52f4";
        TextView textView = (TextView)findViewById(R.id.textView1);
        textView.setOnClickListener(this);
        Log.v("lgq","图片保持。。。。。。。。"+img);

        new Thread(runnable).start();

    }

    Runnable runnable = new Runnable() {

        @Override
        public void run() {
            try {
                bitmap = ((BitmapDrawable) loadImageFromUrl(img)).getBitmap();
                Message msg = new Message();
                msg.what = 1;
                handler.sendMessage(msg);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    };

    private static Drawable loadImageFromUrl(String url) throws IOException {
        URL m = new URL(url);
        InputStream i = (InputStream) m.getContent();
        return Drawable.createFromStream(i, "src");
    }

    //保存方法
    private void saveFile(Bitmap bm, String fileName) throws IOException {
        String subForder = SAVE_REAL_PATH;
        File foder = new File(subForder);
        if (!foder.exists()) foder.mkdirs();

        File myCaptureFile = new File(subForder, fileName);
        Log.v("lgq","图片保持。。。。。。。。"+myCaptureFile);
        Log.v("lgq","图片保持。。。。。getPath。。。"+myCaptureFile.getPath());
        if (!myCaptureFile.exists()) myCaptureFile.createNewFile();
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
        bm.compress(Bitmap.CompressFormat.JPEG, 100, bos);
        bos.flush();
        bos.close();
//        Toast.bottomToast("已保存在/good/savePic目录下", PhotoScanActivity.this);
        //发送广播通知系统
        Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        Uri uri = Uri.fromFile(myCaptureFile);
        intent.setData(uri);
        this.sendBroadcast(intent);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.textView1:
                try {
                    saveFile(bitmap, System.currentTimeMillis() + ".jpg");
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
            default:
                break;
        }
    }

}
    

添加权限

 

 




 

 

 

activity_main.xml

 




    

        

    


    

 

改进版:https://blog.csdn.net/meixi_android/article/details/82801273

 

 

 

你可能感兴趣的:(android,保存图片,浏览图片,图片,移动开发)