2

Ich weiß, das war ein häufiges Problem, aber ich habe verschiedene Vorschläge ausprobiert, und immer noch keine Lösung.Bitmap gespeichert als schwarzes Bild manchmal für die gleiche Bitmap

Mein Problem ist, dass die Bitmap korrekt angezeigt wird, aber manchmal speichert es als eine schwarze Bitmap, während es andere Mal richtig speichert.

Kann jemand sehen, was ich getan habe und mir sagen, wo ich falsch liegen kann?

Ich habe mir die meisten anderen StackOverflow-Fragen angeschaut.

Das folgende ist mein Code, der den Clip-Board-Text in einen QR-Code kodiert und versucht, den erzeugten QR-Code zu speichern.

Vielen Dank!

import android.content.Context; 
import android.content.Intent; 
import android.graphics.Bitmap; 
import android.net.Uri; 
import android.os.Bundle; 
import android.os.Environment; 
import android.support.v7.app.AppCompatActivity; 
import android.text.ClipboardManager; 
import android.view.View; 
import android.widget.ImageView; 
import android.widget.TextView; 
import android.widget.Toast; 

import com.nostra13.universalimageloader.core.DisplayImageOptions; 
import com.nostra13.universalimageloader.core.ImageLoader; 
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration; 

import java.io.File; 
import java.io.FileOutputStream; 
import java.io.UnsupportedEncodingException; 
import java.net.URLEncoder; 

//import android.content.ClipboardManager; 

public class BarcodeWriter extends AppCompatActivity { 
    ImageLoader imgLoader; 
    ImageView qrImg; 
    String copiedStr; 
    TextView qrTxt; 
    ClipboardManager clipboard; 

    String BASE_QR_URL = "http://chart.apis.google.com/chart?cht=qr&chs=400x400&chld=M&choe=UTF-8&chl="; 
    String fullUrl = BASE_QR_URL; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.barcode_writer); 

     DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder() 
       .cacheInMemory(true) 
       .cacheOnDisk(true) 
       .build(); 

     ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext()) 
       .defaultDisplayImageOptions(defaultOptions) 
       .build(); 
     imgLoader = ImageLoader.getInstance(); // Do it on Application start 
     imgLoader.init(config); 

     qrImg = (ImageView)findViewById(R.id.qrImg); 
     qrTxt = (TextView)findViewById(R.id.qrTxt); 

     clipboard = (ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE); 

     /* 
     * clipboard.getText() is now deprecated. But I am going to use it here 
     * because the new way of doing the same thing only works on API lvl 11+ 
     * Since I want this application to support API lvl 4+ we have to use 
     * the old method. 
     */ 
     CharSequence clipTxt = clipboard.getText(); 

     //This is the new, non-deprecated way of getting text from the Clipboard. 
     //CharSequence clipTxt = clipboard.getPrimaryClip().getItemAt(0).getText(); 


     //If the clipboard has text, and it is more than 0 characters. 
     if((null != clipTxt) && (clipTxt.length() > 0)){ 
      try { 
       qrTxt.setText(clipTxt); 
       copiedStr = clipTxt.toString(); 
       fullUrl += URLEncoder.encode(copiedStr, "UTF-8"); 
       //imgLoader.displayImage(fullUrl, qrImg); 


       ImageLoader.getInstance().displayImage(fullUrl, qrImg, defaultOptions); // Incoming options will be used 

       qrImg.setDrawingCacheEnabled(true); 

       qrImg.measure(View.MeasureSpec.makeMeasureSpec(100, View.MeasureSpec.EXACTLY), 
         View.MeasureSpec.makeMeasureSpec(100, View.MeasureSpec.EXACTLY)); 
       qrImg.layout(0, 0, qrImg.getMeasuredWidth(), qrImg.getMeasuredHeight()); 

       qrImg.buildDrawingCache(); 
       Bitmap bm = Bitmap.createBitmap(qrImg.getDrawingCache()); 
       qrImg.setDrawingCacheEnabled(false); // clear drawing cache 


       try { 

        String root = Environment.getExternalStorageDirectory().toString(); 
        File myDir = new File(root + "/DCIM/QR codes"); 
        myDir.mkdirs(); 


        String fname = clipTxt+".png"; 
        File file = new File (myDir, fname); 

        if (file.exists()) file.delete(); 
        try { 
         FileOutputStream out = new FileOutputStream(file); 
         bm.compress(Bitmap.CompressFormat.PNG, 100, out); 
         Toast.makeText(BarcodeWriter.this, "Image Saved", Toast.LENGTH_SHORT).show(); 
         out.flush(); 
         out.close(); 

        } catch (Exception e) { 
         e.printStackTrace(); 
        } 

        Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); 
        Uri contentUri = Uri.fromFile(file); 
        mediaScanIntent.setData(contentUri); 
        getApplicationContext().sendBroadcast(mediaScanIntent); 

        Toast.makeText(this,"QR Code showing "+clipTxt,Toast.LENGTH_SHORT).show(); 
       } catch (Exception e) { 
        Toast.makeText(this, "Error occurred. Please try again later.", 
          Toast.LENGTH_SHORT).show(); 
       } 



      } catch (UnsupportedEncodingException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

     }else{ //If no text display a dialog. 

      Toast.makeText(this,"No text in clipboard",Toast.LENGTH_SHORT).show(); 
     } 
    } 
} 

Antwort

1

Ihr Code scheint das einzige, was ich sah, in Ordnung zu sein, ist, dass man Sie aus und externe URL mit Universal-Image Loader Bitmap werden immer sofort nach displayImage aufrufen. So ist es möglich, dass für einige Verzögerungen im Netzwerk manchmal Ihre ImageView immer noch nicht das gesamte Bild und können Sie Ihre Bitmap als schwarz gespeichert.

Bitte versuchen Sie den gesamten Code unten ImageLoader.getInstance().displayImage innerhalb des onLoadingComplete wie folgt bewegen:

ImageLoader.getInstance().displayImage(fullUrl, qrImg, defaultOptions, new SimpleImageLoadingListener() 
    {   

     @Override 
     public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) { 
      ImageLoader.getInstance().displayImage(fullUrl, qrImg, defaultOptions,null); 
      qrImg.setDrawingCacheEnabled(true); 
      ... 
      catch (Exception e) { 
       Toast.makeText(this, "Error occurred. Please try again later.", 
         Toast.LENGTH_SHORT).show(); 
      } 

     } 
    }); 

Hope this Hilfe !!

+0

Das hat funktioniert! Vielen Dank! – User404