前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >获取本地图片并显示「建议收藏」

获取本地图片并显示「建议收藏」

作者头像
全栈程序员站长
发布2022-07-07 20:09:38
7400
发布2022-07-07 20:09:38
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是全栈君。

代码语言:javascript
复制
package com.example.showgetpic;

import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

public class MainActivity extends Activity {
	private ImageView iv;
	private OnClickListener imgViewListener;
	private Bitmap myBitmap;
	private int REQUEST_OK = 1;
	private LinearLayout ly_list;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		iv = (ImageView) findViewById(R.id.iv);
		ly_list=(LinearLayout) findViewById(R.id.ly_list);
		iv.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();  
				//intent = new Intent(Intent.ACTION_GET_CONTENT);
                /* 开启Pictures画面Type设定为image */  
                intent.setType("image/*");  
                /* 使用Intent.ACTION_GET_CONTENT这个Action */  
                intent.setAction(Intent.ACTION_GET_CONTENT);   
                /* 取得相片后返回本画面 */  
                startActivityForResult(intent, 1);  

			}
		});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		if (requestCode	 == REQUEST_OK) {
			Uri selectedImage = data.getData();
			try {
				Bitmap bitmap = BitmapFactory.decodeStream(this
						.getContentResolver().openInputStream(Uri.parse(selectedImage.toString())));
				iv.setImageBitmap(bitmap);
				Toast.makeText(getApplicationContext(), "上传成功",
						Toast.LENGTH_SHORT).show();
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

//	public static Bitmap getPicFromBytes(byte[] bytes,
//			BitmapFactory.Options opts) {
//		if (bytes != null)
//			if (opts != null)
//				return BitmapFactory.decodeByteArray(bytes, 0, bytes.length,
//						opts);
//			else
//				return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
//		return null;
//	}
//
//	public static byte[] readStream(InputStream in) throws Exception {
//		byte[] buffer = new byte[1024];
//		int len = -1;
//		ByteArrayOutputStream outStream = new ByteArrayOutputStream();
//
//		while ((len = in.read(buffer)) != -1) {
//			outStream.write(buffer, 0, len);
//		}
//		byte[] data = outStream.toByteArray();
//		outStream.close();
//		in.close();
//		return data;
//	}

}

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/116317.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年1月2,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档