com.xyzlf.share:sharesdk---getSampleBitmap--- BitmapFactory.decodeStream返回值為空
阿新 • • 發佈:2019-02-17
https://github.com/xyzlf/ShareSDK bug檔案
import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.util.Log; import java.io.BufferedInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; /** * Created by zhanglifeng on 1/6/17*/ public class BitmapAsyncTask extends AbstractAsyncTask<Bitmap> { private String urlStr; private OnBitmapListener listener; public BitmapAsyncTask(String urlStr, OnBitmapListener listener) { this.urlStr = urlStr; this.listener = listener; } @Override protected Bitmap doLoadData() throws Exception{ URL url = new URL(urlStr); InputStream is = url.openStream(); Bitmap bitmap = null; if (is == null) { throw new RuntimeException("stream is null"); } else { try { byte[] data = readStream(is); if (data != null) { bitmap = getSampleBitmap2(data, 640, 640); } } catch (Exceptione) { e.printStackTrace(); } is.close(); } return bitmap; } private Bitmap getSampleBitmap2(byte[] data, int width, int height) { final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeByteArray(data, 0, data.length, options); calculateInSampleSize(width, height, options, true); return BitmapFactory.decodeByteArray(data, 0, data.length, options); } /* * 得到圖片位元組流 陣列大小 */ public static byte[] readStream(InputStream inStream) throws Exception { ByteArrayOutputStream outStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = 0; while ((len = inStream.read(buffer)) != -1) { outStream.write(buffer, 0, len); } outStream.close(); inStream.close(); return outStream.toByteArray(); } /** * * @param is 第二次壓縮容易為空 * @param width * @param height * @return */ private Bitmap getSampleBitmap(InputStream is, int width, int height) { BufferedInputStream stream = new BufferedInputStream(is); stream.mark(4 * 1024); final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeStream(stream, null, options); calculateInSampleSize(width, height, options, true); try { stream.reset(); } catch (IOException e) { } return BitmapFactory.decodeStream(stream, null, options); } static void calculateInSampleSize(int reqWidth, int reqHeight, BitmapFactory.Options options, boolean centerInside) { calculateInSampleSize(reqWidth, reqHeight, options.outWidth, options.outHeight, options, centerInside); } static void calculateInSampleSize(int reqWidth, int reqHeight, int width, int height, BitmapFactory.Options options, boolean centerInside) { int sampleSize = 1; if (height > reqHeight || width > reqWidth) { final int heightRatio; final int widthRatio; if (reqHeight == 0) { sampleSize = (int) Math.floor((float) width / (float) reqWidth); } else if (reqWidth == 0) { sampleSize = (int) Math.floor((float) height / (float) reqHeight); } else { heightRatio = (int) Math.floor((float) height / (float) reqHeight); widthRatio = (int) Math.floor((float) width / (float) reqWidth); sampleSize = centerInside ? Math.max(heightRatio, widthRatio) : Math.min(heightRatio, widthRatio); } } Log.e("resulttt", "=====sampleSize================" + sampleSize); options.inSampleSize = sampleSize; options.inJustDecodeBounds = false; } @Override public void onSuccess(Bitmap bitmap) { super.onSuccess(bitmap); if (null != listener) { listener.onSuccess(bitmap); } } @Override public void onException(Exception exception) { super.onException(exception); if (null != listener) { listener.onException(exception); } } @Override public void onFinally() { super.onFinally(); } public interface OnBitmapListener { void onSuccess(Bitmap bitmap); void onException(Exception exception); } }