Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/190.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 下载谷歌静态地图后,你能在画布上绘制它吗?_Android_Canvas_Bitmap_Google Static Maps - Fatal编程技术网

Android 下载谷歌静态地图后,你能在画布上绘制它吗?

Android 下载谷歌静态地图后,你能在画布上绘制它吗?,android,canvas,bitmap,google-static-maps,Android,Canvas,Bitmap,Google Static Maps,我正在使用下面的类下载一个特定的地图,我想将它与其他位图一起绘制到画布上,但当我这样做时,只有地图不会显示。 我知道地图正在下载,我是否必须使其可变,我不完全确定这里发生了什么 class TheTask extends AsyncTask<Void,Void,Void> { File mediaFile = new File(MenuScreen.mediaStorageDir.getPath() + File.separator + "I

我正在使用下面的类下载一个特定的地图,我想将它与其他位图一起绘制到画布上,但当我这样做时,只有地图不会显示。 我知道地图正在下载,我是否必须使其可变,我不完全确定这里发生了什么

 class TheTask extends AsyncTask<Void,Void,Void>
    {
    File mediaFile = new File(MenuScreen.mediaStorageDir.getPath() + File.separator +
            "IMG_"+ MenuScreen.timeStamp + ".png");
    File mediaFile1 = new File(MenuScreen.mediaStorageDir.getPath() + File.separator +
            "IMG_"+ MenuScreen.timeStamp + "1" + ".png");
    File mediaFile2 = new File(MenuScreen.mediaStorageDir.getPath() + File.separator +
            "IMG_"+ MenuScreen.timeStamp + "2" + ".png");
    Bitmap bitmap;
          protected void onPreExecute()
          {           super.onPreExecute();
                    //display progressdialog.
          } 

           protected Void doInBackground(Void ...params)
          {  





            bitmap = getGoogleMapThumbnail(MainActivity.latlng.latitude,MainActivity.latlng.longitude);

          FileOutputStream outStream;
            try {
                outStream = new FileOutputStream(mediaFile2);
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
                outStream.flush();
                outStream.close();
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


            //mapView.setDrawingCacheEnabled(enabled);
            //mediaFile = new File(MenuScreen.mediaStorageDir.getPath() + File.separator +
                   // "IMG_"+ MenuScreen.timeStamp + ".jpg");

                return null;
          } 

           protected void onPostExecute(Void result)
          {     

                    super.onPostExecute(result);
                    Bitmap finished;
                       String path = mediaFile1.getAbsolutePath();
                       String path1 = mediaFile2.getAbsolutePath();
                       Bitmap map = BitmapFactory.decodeFile(path1);
                       Bitmap photo = BitmapFactory.decodeFile(path);
                       Paint into = new Paint();
                         into.setFilterBitmap(true);

                            into.setColor(Color.BLACK);
                            into.setTextSize(25);
                            into.setUnderlineText(true);
                            into.setTypeface(Typeface.DEFAULT_BOLD);
                            Bitmap datetime = Bitmap.createBitmap(200,200,Bitmap.Config.ARGB_8888);
                            Canvas can = new Canvas(datetime);
                            can.drawColor(Color.WHITE);
                            can.drawText(MainActivity.curDate, 0, 35, into);
                            can.drawText(MainActivity.curTime, 0, 70, into);
                          if(photo.getWidth() > photo.getHeight()){

                            finished = Bitmap.createBitmap(480,720,Bitmap.Config.ARGB_8888);

                            Canvas ca = new Canvas(finished);
                            ca.drawColor(Color.WHITE);
                            ca.drawBitmap(map,0, 0, null);
                            ca.drawBitmap(datetime, 0, 0, null);

                            //ca.drawBitmap(bm, new Rect(0,0, bm.getWidth(), bm.getHeight()),new Rect(280,0,480,200) , null);

                            ca.drawBitmap(photo, 0, 400, null);
                        }else{

                            finished = Bitmap.createBitmap(720,480,Bitmap.Config.ARGB_8888);
                            Canvas ca = new Canvas(finished);
                            ca.drawColor(Color.WHITE);
                            ca.drawBitmap(map, 320, 0, null);
                            ca.drawBitmap(datetime, 320, 0, null);

                            //ca.drawBitmap(bm, new Rect(0,0, bm.getWidth(), bm.getHeight()),new Rect(320,280,520,480) , null);

                            ca.drawBitmap(photo, 0, 0, null);
                        }
                          mediaFile1.delete();
                          mediaFile2.delete();
                         FileOutputStream outStream;
                        try {
                            outStream = new FileOutputStream(mediaFile);
                            finished.compress(Bitmap.CompressFormat.PNG, 100, outStream);
                            outStream.flush();
                            outStream.close();
                        } catch (FileNotFoundException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }

                        //sendBroadcast(new Intent(
                            //    Intent.ACTION_MEDIA_MOUNTED,
                              //  Uri.parse("file://" + Environment.getExternalStorageDirectory())));


                    //dismiss progressdialog.
                    //update ui
          } 
           public static Bitmap getGoogleMapThumbnail(double lati, double longi){
               String URL = "http://maps.google.com/maps/api/staticmap?center=" +lati + "," + longi + "&zoom=15&size=200x200&sensor=false";
               Bitmap bmp = null;
               bmp = getBitmapFromURL(URL);

               /*HttpClient httpclient = new DefaultHttpClient();   
               HttpGet request = new HttpGet(URL); 

               InputStream in = null;
               try {
                   in = httpclient.execute(request).getEntity().getContent();
                   bmp = BitmapFactory.decodeStream(in);
                   in.close();
               } catch (IllegalStateException e) {
                   // TODO Auto-generated catch block
                   e.printStackTrace();
               } catch (ClientProtocolException e) {
                   // TODO Auto-generated catch block
                   e.printStackTrace();
               } catch (IOException e) {
                   // TODO Auto-generated catch block
                   e.printStackTrace();
               }*/

               return bmp;
           }
           public static Bitmap getBitmapFromURL(String src) {
            try {
                URL url = new URL(src);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setDoInput(true);
                connection.connect();
                InputStream input = connection.getInputStream();
                Bitmap myBitmap = BitmapFactory.decodeStream(input);
                return myBitmap;
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
        }
class任务扩展异步任务
{
File mediaFile=新文件(MenuScreen.mediaStorageDir.getPath()+File.separator+
“IMG_”+MenuScreen.timeStamp+“.png”);
File mediaFile1=新文件(MenuScreen.mediaStorageDir.getPath()+File.separator+
“IMG_”+MenuScreen.timeStamp+“1”+“.png”);
File mediaFile2=新文件(MenuScreen.mediaStorageDir.getPath()+File.separator+
“IMG_”+MenuScreen.timeStamp+“2”+“.png”);
位图;
受保护的void onPreExecute()
{super.onPreExecute();
//显示进度对话框。
} 
受保护的Void doInBackground(Void…参数)
{  
位图=getGoogleMapThumbnail(MainActivity.latlng.latitude,MainActivity.latlng.longitude);
FileOutputStream扩展流;
试一试{
outStream=新文件OutputStream(mediaFile2);
compress(bitmap.CompressFormat.PNG,100,扩展);
冲水;
exptream.close();
}catch(filenotfounde异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
//mapView.setDrawingCacheEnabled(已启用);
//mediaFile=新文件(MenuScreen.mediaStorageDir.getPath()+File.separator+
//“IMG_”+MenuScreen.timeStamp+”.jpg”);
返回null;
} 
受保护的void onPostExecute(void结果)
{     
super.onPostExecute(结果);
位图完成;
字符串路径=mediaFile1.getAbsolutePath();
字符串路径1=mediaFile2.getAbsolutePath();
位图映射=位图工厂.decodeFile(路径1);
位图照片=BitmapFactory.decodeFile(路径);
绘制成=新的绘制();
into.setFilterBitmap(true);
into.setColor(Color.BLACK);
进入.setTextSize(25);
into.setUnderlineText(true);
into.setTypeface(Typeface.DEFAULT_BOLD);
Bitmap datetime=Bitmap.createBitmap(200200,Bitmap.Config.ARGB_8888);
Canvas can=新画布(日期时间);
can.drawColor(颜色:白色);
can.drawText(MainActivity.curDate,0,35,into);
can.drawText(MainActivity.curTime,0,70,into);
if(photo.getWidth()>photo.getHeight()){
finished=Bitmap.createBitmap(480720,Bitmap.Config.ARGB_8888);
画布ca=新画布(已完成);
ca.drawColor(颜色为白色);
ca.drawBitmap(map,0,0,null);
ca.drawBitmap(日期时间,0,0,null);
//ca.drawBitmap(bm,新矩形(0,0,bm.getWidth(),bm.getHeight()),新矩形(280,0480200),null);
ca.drawBitmap(照片,0,400,空);
}否则{
finished=Bitmap.createBitmap(720480,Bitmap.Config.ARGB_8888);
画布ca=新画布(已完成);
ca.drawColor(颜色为白色);
ca.drawBitmap(map,320,0,null);
ca.drawBitmap(日期时间,320,0,空);
//ca.drawBitmap(bm,新矩形(0,0,bm.getWidth(),bm.getHeight()),新矩形(320280520480),null);
ca.drawBitmap(照片,0,0,空);
}
mediaFile1.delete();
mediaFile2.delete();
FileOutputStream扩展流;
试一试{
outStream=新文件OutputStream(mediaFile);
compress(Bitmap.CompressFormat.PNG,100,扩展);
冲水;
exptream.close();
}catch(filenotfounde异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
//发送广播(新意图)(
//意图.行动\媒体\安装,
//parse(“文件:/”+Environment.getExternalStorageDirectory());
//关闭进程对话框。
//更新用户界面
} 
公共静态位图getGoogleMapThumbnail(双lati,双longi){
字符串URL=”http://maps.google.com/maps/api/staticmap?center=“+lati+”、“+longi+”&zoom=15&size=200x200&sensor=false”;
位图bmp=null;
bmp=getBitmapFromURL(URL);
/*HttpClient HttpClient=新的DefaultHttpClient();
HttpGet请求=新的HttpGet(URL);
InputStream in=null;
试一试{
in=httpclient.execute(request).getEntity().getContent();
bmp=位图工厂.decodeStream(in);
in.clo
class TheTask extends AsyncTask<Void, Void, Bitmap> {
    File mediaFile = new File(MenuScreen.mediaStorageDir.getPath() + File.separator + "IMG_" + MenuScreen.timeStamp + ".png");
    File mediaFile1 = new File(MenuScreen.mediaStorageDir.getPath() + File.separator + "IMG_" + MenuScreen.timeStamp + "1" + ".png");
    //File mediaFile2 = new File(MenuScreen.mediaStorageDir.getPath() + File.separator + "IMG_" + MenuScreen.timeStamp + "2" + ".png");
    Bitmap mapBitmap;

    protected void onPreExecute() {
        super.onPreExecute();
        // display progressdialog.
    }

    protected Bitmap doInBackground(Void... params) {
        mapBitmap = getGoogleMapThumbnail(MainActivity.latlng.latitude, MainActivity.latlng.longitude);
        // You could reuse this bitmap. Rather than saving and loading again
        return mapBitmap;
    }

    protected void onPostExecute(Bitmap map) {
        super.onPostExecute(map);

        Bitmap finished;

        String path = mediaFile1.getAbsolutePath();

        Bitmap photo = BitmapFactory.decodeFile(path);

        Paint into = new Paint();

        Paint background = new Paint();
        background.setColor(Color.WHITE);
        background.setStyle(Style.FILL);

        into.setFilterBitmap(true);

        into.setColor(Color.BLACK);
        into.setTextSize(25);
        into.setUnderlineText(true);
        into.setTypeface(Typeface.DEFAULT_BOLD);

        // Bitmap datetime = Bitmap.createBitmap(200, 200,
        // Bitmap.Config.ARGB_8888);
        //
        // Canvas can = new Canvas(datetime);
        // can.drawColor(Color.WHITE);
        // can.drawText(MainActivity.curDate, 0, 35, into);
        // can.drawText(MainActivity.curTime, 0, 70, into);

        Rect rect = new Rect();

        if (photo.getWidth() > photo.getHeight()) {

            finished = Bitmap.createBitmap(480, 720, Bitmap.Config.ARGB_8888);

            Canvas ca = new Canvas(finished);
            ca.drawColor(Color.WHITE);

            ca.drawBitmap(map, 0, 0, null);

            rect.set(0, 0, 200, 200);
            ca.drawRect(rect, background);
            ca.drawText(MainActivity.curDate, 0, 35, into);
            ca.drawText(MainActivity.curTime, 0, 70, into);

            ca.drawBitmap(photo, 0, 400, null);
        }
        else {

            finished = Bitmap.createBitmap(720, 480, Bitmap.Config.ARGB_8888);

            Canvas ca = new Canvas(finished);
            ca.drawColor(Color.WHITE);

            ca.drawBitmap(map, 320, 0, null);

            ca.save();

            ca.translate(320, 0);
            rect.set(0, 0, 200, 200);
            ca.drawRect(rect, background);
            ca.drawText(MainActivity.curDate, 0, 35, into);
            ca.drawText(MainActivity.curTime, 0, 70, into);

            ca.restore();

            ca.drawBitmap(photo, 0, 0, null);
        }

        mediaFile1.delete();

        FileOutputStream outStream;
        try {
            outStream = new FileOutputStream(mediaFile);
            finished.compress(Bitmap.CompressFormat.PNG, 100, outStream);
            outStream.flush();
            outStream.close();
        }
        catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static Bitmap getGoogleMapThumbnail(double lati, double longi) {
        String URL = "http://maps.google.com/maps/api/staticmap?center=" + lati + "," + longi + "&zoom=15&size=200x200&sensor=false";
        Bitmap bmp = null;
        bmp = getBitmapFromURL(URL);
        return bmp;
    }

    public static Bitmap getBitmapFromURL(String src) {
        try {
            URL url = new URL(src);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            Bitmap myBitmap = BitmapFactory.decodeStream(input);
            return myBitmap;
        }
        catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}