我是Android World的新手。 我将开始说我知道我编写了错误的代码,我的目的是#34;让它先工作然后让它变得更好",至少对于非常小的项目来说。 我使用毕加索图书馆下载图片。 好吧,它的作品!但现在我想把ImageView放在一个Bitmap对象中,这样我就可以保存它。
ViewGroup layout = (ViewGroup) findViewById(R.id.activity_display_message);
ImageView iview = new ImageView(cc);
Picasso.with(cc).load(myUrl).into(iview);
我还没有理解上下文的概念,所以我只是让毕加索作品。 作为一个全局变量我声明:
Context cc;
我在onCreate函数中使用了
cc = this;
那是因为我在一个扩展AsyncTask的课程中调用Picasso而只是用(这个)写作是没有用的。我觉得这是一个不好的做法! 无论如何它是有效的,但当我尝试:
Bitmap bmx = iview.getDrawingCache();
bmx变为空。
我尝试建立缓存:
iview.buildDrawingCache();
bmx = iview.getDrawingCache();
iview.destroyDrawingCache();
使用我发现的功能:
public static Bitmap loadBitmapFromView(View v)
{
Bitmap b = Bitmap.createBitmap( v.getLayoutParams().width, v.getLayoutParams().height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.layout(0, 0, v.getLayoutParams().width, v.getLayoutParams().height);
v.draw(c);
return b;
}
通过BitmapDrawable:
BitmapDrawable drawable = (BitmapDrawable) iview.getDrawable();
Bitmap bitmap = drawable.getBitmap();
和此:
Bitmap bitmap = ((BitmapDrawable)iview.getDrawable()).getBitmap();
公开启用缓存:
iview.setDrawingCacheEnabled(true);
Bitmap bmx = iview.getDrawingCache();
没有运气。最后,我把Picasso扔给了这个家伙:DownloadManager。
我想知道自己出了什么问题,如果你们有空闲时间,我很乐意听一些好的做法!就像停止使用AsyncTask下载像我一样的图像并开始使用intentservice。
这是我的完整错误代码:
> package com.cmandracchia.template;
>
> import android.app.Activity; import android.app.DownloadManager;
> import android.content.Context; import android.content.Intent; import
> android.content.pm.PackageManager; import android.graphics.Bitmap;
> import android.graphics.Canvas; import
> android.graphics.drawable.BitmapDrawable; import android.net.Uri;
> import android.os.AsyncTask; import android.os.Build; import
> android.os.Environment; import android.support.v4.app.ActivityCompat;
> import android.support.v4.content.ContextCompat; import
> android.support.v7.app.AppCompatActivity; import android.os.Bundle;
> import android.view.View; import android.view.ViewGroup; import
> android.widget.ImageView; import android.widget.Toast;
>
> import com.squareup.picasso.Picasso;
>
> import java.io.BufferedReader; import java.io.File; import
> java.io.FileOutputStream; import java.io.IOException; import
> java.io.InputStreamReader; import java.net.URL; import
> java.util.regex.Matcher; import java.util.regex.Pattern;
>
> public class DisplayMessageActivity extends AppCompatActivity {
> String myUrl;
> Context cc;
> Bitmap bmx;
>
> @Override
> protected void onCreate(Bundle savedInstanceState)
> {
> super.onCreate(savedInstanceState);
> setContentView(R.layout.activity_display_message);
>
> Intent intent = getIntent();
> String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
>
> cc = this;
> if(Build.VERSION.SDK_INT >= 23)
> {
> if(checkPermission())
> {
> // Code for above or equal 23 API Oriented Device
> // Create a common Method for both
> }
> else
> {
> requestPermission();
> }
> }
> else
> {
> // Code for Below 23 API Oriented Device
> // Create a common Method for both
> }
>
> RetrievePage rp = new RetrievePage();
> rp.execute(message);
> }
>
> public class RetrievePage extends AsyncTask<String, Void, String>
> {
> protected String doInBackground(String... urls)
> {
> StringBuilder sb = new StringBuilder();
> try
> {
> URL url = new URL(urls[0]);
> BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
> String str;
> while ((str = in.readLine()) != null)
> {
> sb.append(str);
> }
> in.close();
> }
> catch(IOException e)
> {
> e.printStackTrace();
> }
> return sb.toString();
> }
>
> @Override
> protected void onPostExecute(String ss)
> {
> // here there is some manipulation that you don't see because i deleted for keeping the code short
> myUrl = ss;
>
> ViewGroup layout = (ViewGroup) findViewById(R.id.activity_display_message);
> ImageView iview = new ImageView(cc);
> Picasso.with(cc).load(myUrl).into(iview);
>
> layout.addView(iview);
> iview.buildDrawingCache();
> bmx = iview.getDrawingCache();
> iview.destroyDrawingCache();
>
>
> if(bmx == null)
> {System.out.println("bmx is null");}
> else
> {System.out.println("bmx is not null");}
>
> try
> {
> File image = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),
> "Example.jpg");
> FileOutputStream out = new FileOutputStream(image);
>
> bmx.compress(Bitmap.CompressFormat.JPEG, 100, out);
> out.flush();
> out.close();
> System.out.println("it's over");
> }
> catch(Exception e)
> {
> e.printStackTrace();
> }
> }
> }
>
> private boolean checkPermission()
> {
> int result = ContextCompat.checkSelfPermission(cc, android.Manifest.permission.WRITE_EXTERNAL_STORAGE);
> if (result == PackageManager.PERMISSION_GRANTED)
> {
> return true;
> }
> else
> {
> return false;
> }
> }
>
> private void requestPermission()
> {
> if (ActivityCompat.shouldShowRequestPermissionRationale(cce, android.Manifest.permission.WRITE_EXTERNAL_STORAGE))
> {
> Toast.makeText(cc, "Write External Storage permission allows us to do store images. Please allow this permission in App
> Settings.", Toast.LENGTH_LONG).show();
> } else {
> ActivityCompat.requestPermissions(cce, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
> }
> }
>
> @Override
> public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults)
> {
> switch (requestCode)
> {
> case 1:
> if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
> {
> System.out.println("Permission Granted, Now you can use local drive .");
> }
> else
> {
> System.out.println("Permission Denied, You cannot use local drive .");
> }
> break;
> }
> }
>
> }
感谢!!!