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;
        }
    }

}
    

添加权限

 

 

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<uses-permission android:name="android.permission.INTERNET" />

 

 

 

activity_main.xml

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#00050B"
    android:orientation="vertical">

    <LinearLayout
        android:layout_height="45dp"
        android:layout_width="fill_parent"
        android:gravity="center"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="保存" />

    </LinearLayout>


    <uk.co.senab.photoview.PhotoView
        android:id="@+id/iv_photo"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</LinearLayout>

 

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

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值