2016-07-21 8 views
0

Ich versuche, die Speicherfunktion der MPAndroidChart-Bibliothek aufzurufen, um ein Diagramm zu speichern, das ich erstellt habe. Ich kann das Diagramm in Ordnung schaffen, aber wenn ich versuche, tritt das Diagramm folgendes zu speichern:Problem beim Speichern von Diagramm in Android Studio

07-21 14:52:42.879 5818-5818/com.example.a1003137m.profitcalculator E/AndroidRuntime: FATAL EXCEPTION: main 
    Process: com.example.a1003137m.profitcalculator, PID: 5818 
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.a1003137m.profitcalculator/com.example.a1003137m.profitcalculator.ChartActivity}: java.lang.IllegalArgumentException: width and height must be > 0 
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2426) 
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490) 
     at android.app.ActivityThread.-wrap11(ActivityThread.java) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354) 
     at android.os.Handler.dispatchMessage(Handler.java:102) 
     at android.os.Looper.loop(Looper.java:148) 
     at android.app.ActivityThread.main(ActivityThread.java:5443) 
     at java.lang.reflect.Method.invoke(Native Method) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
    Caused by: java.lang.IllegalArgumentException: width and height must be > 0 
     at android.graphics.Bitmap.createBitmap(Bitmap.java:855) 
     at android.graphics.Bitmap.createBitmap(Bitmap.java:834) 
     at android.graphics.Bitmap.createBitmap(Bitmap.java:801) 
     at com.github.mikephil.charting.charts.Chart.getChartBitmap(Chart.java:1492) 
     at com.github.mikephil.charting.charts.Chart.saveToGallery(Chart.java:1597) 
     at com.github.mikephil.charting.charts.Chart.saveToGallery(Chart.java:1636) 
     at com.example.a1003137m.profitcalculator.ChartActivity.onCreate(ChartActivity.java:55) 
     at android.app.Activity.performCreate(Activity.java:6245) 
     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1130) 
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379) 
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490) 
     at android.app.ActivityThread.-wrap11(ActivityThread.java) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354) 
     at android.os.Handler.dispatchMessage(Handler.java:102) 
     at android.os.Looper.loop(Looper.java:148) 
     at android.app.ActivityThread.main(ActivityThread.java:5443) 
     at java.lang.reflect.Method.invoke(Native Method) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 

Ich kann sehen, dass das Problem der illegalen Argument Ausnahme ist, würde aber jedem passieren wissen, warum dies geworfen wird?

Ich habe die Berechtigungen in meinem Manifest und ich fordere sie auch beim Laufen. Hier

ist die Linie in Frage, wo die oben stacktrace auftritt:

mChart.saveToGallery("chart",50);

Aktivitätscode:

package com.example.a1003137m.profitcalculator; 

import android.Manifest; 
import android.app.Activity; 
import android.content.Intent; 
import android.content.pm.PackageManager; 
import android.graphics.Bitmap; 
import android.os.Environment; 
import android.support.v4.app.ActivityCompat; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.util.Log; 
import android.widget.RelativeLayout; 

import com.github.mikephil.charting.charts.BarChart; 
import com.github.mikephil.charting.components.XAxis; 
import com.github.mikephil.charting.data.BarData; 
import com.github.mikephil.charting.data.BarDataSet; 
import com.github.mikephil.charting.data.BarEntry; 
import com.github.mikephil.charting.data.Entry; 
import com.github.mikephil.charting.formatter.ValueFormatter; 
import com.github.mikephil.charting.formatter.XAxisValueFormatter; 
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet; 
import com.github.mikephil.charting.utils.ColorTemplate; 
import com.github.mikephil.charting.utils.ViewPortHandler; 

import java.io.File; 
import java.util.ArrayList; 
import java.util.List; 

public class ChartActivity extends AppCompatActivity { 


    protected BarChart mChart; 
    private String choice; 
    // Storage Permissions 
    private static final int REQUEST_EXTERNAL_STORAGE = 1; 
    private static String[] PERMISSIONS_STORAGE = { 
      Manifest.permission.READ_EXTERNAL_STORAGE, 
      Manifest.permission.WRITE_EXTERNAL_STORAGE 
    }; 



    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_chart); 
     Intent intent = getIntent(); 
     choice = intent.getStringExtra("choice"); 
     Bundle data_bundle = intent.getBundleExtra("bundle"); 
     verifyStoragePermissions(this); 
     boolean check = setChart(data_bundle); 
     if (check){ 
      mChart.saveToGallery("chart",50); 
     } 
    } 



    private boolean setChart(Bundle data_bundle) { 

     mChart = (BarChart) findViewById(R.id.chart); 
     mChart.setDescription("Test"); 
     ArrayList<String> year = (ArrayList<String>) data_bundle.getSerializable("year"); 
     ArrayList<Double> depreciation = (ArrayList<Double>) data_bundle.getSerializable("dep"); 
     ArrayList<Integer> population = (ArrayList<Integer>) data_bundle.getSerializable("pop"); 


     ArrayList<BarEntry> dep_entries = new ArrayList<>(); 
     ArrayList<BarEntry> pop_entries = new ArrayList<>(); 
     for (int i = 0; i< depreciation.size(); i++){ 
      BarEntry entry = new BarEntry(depreciation.get(i).floatValue(),i); 
      dep_entries.add(entry); 
     } 
     for (int i = 0; i< population.size(); i++){ 
      BarEntry entry = new BarEntry(population.get(i).floatValue(), i); 
      pop_entries.add(entry); 
     } 

     BarDataSet barDep = new BarDataSet(pop_entries,"Population"); 
     barDep.setColors(ColorTemplate.COLORFUL_COLORS); 
     BarDataSet barPop = new BarDataSet(dep_entries, "Depreciation"); 
     barDep.setColors(ColorTemplate.JOYFUL_COLORS); 

     List<IBarDataSet> sets = new ArrayList<>(); 
     sets.add(barDep); 
     sets.add(barPop); 

     mChart.setDescription("Bar Chart of data for "+choice); 
     BarData bar_data = new BarData(year, sets); 
     mChart.setData(bar_data); 
     boolean success =true; 
     return success; 


    } 

    /** 
    * Checks if the app has permission to write to device storage 
    * 
    * If the app does not has permission then the user will be prompted to grant permissions 
    * 
    * @param activity 
    */ 
    public static void verifyStoragePermissions(Activity activity) { 
     // Check if we have write permission 
     int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE); 

     if (permission != PackageManager.PERMISSION_GRANTED) { 
      // We don't have permission so prompt the user 
      ActivityCompat.requestPermissions(
        activity, 
        PERMISSIONS_STORAGE, 
        REQUEST_EXTERNAL_STORAGE 
      ); 
     } 
    } 




} 
+0

Sie müssen sonst ein Codebeispiel zur Verfügung zu stellen, es ist schwer von nur einer Linie zu erzählen. Das einzige, was ich sagen kann, ist sicherzustellen, dass das Diagramm gerendert wird, bevor versucht wird, den Fehler zu speichern, den Sie bekommen, weil eine Bitmap eine Höhe oder Breite von Null hat. – Ben

+0

Ich vermutete, dass dies das Problem ist. Haben Sie Empfehlungen, wie Sie warten können, bis das Diagramm bis zum Speichern gerendert wird? – cmackie21

+0

Schwer zu sagen, ohne Ihren Workflow zu kennen. Vielleicht deaktivieren Sie eine Schaltfläche zum Speichern, bis es gerendert wird. Sie könnten versuchen, vor dem Speichern invalidate() aufzurufen. – Ben

Antwort

0

ich das Problem gelöst haben, indem Sie eine Taste, um mein Layout hinzugefügt, die das spart Grafik beim Anklicken. Dadurch wird sichergestellt, dass das Diagramm vollständig gerendert und angehängt ist, bevor Sie es auf Ihrem Gerät speichern.

Voll Code:

package com.example.a1003137m.profitcalculator; 

import android.Manifest; 
import android.app.Activity; 
import android.content.Intent; 
import android.content.pm.PackageManager; 
import android.graphics.Bitmap; 
import android.os.Environment; 
import android.support.v4.app.ActivityCompat; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.RelativeLayout; 

import com.github.mikephil.charting.charts.BarChart; 
import com.github.mikephil.charting.components.XAxis; 
import com.github.mikephil.charting.data.BarData; 
import com.github.mikephil.charting.data.BarDataSet; 
import com.github.mikephil.charting.data.BarEntry; 
import com.github.mikephil.charting.data.Entry; 
import com.github.mikephil.charting.formatter.ValueFormatter; 
import com.github.mikephil.charting.formatter.XAxisValueFormatter; 
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet; 
import com.github.mikephil.charting.utils.ColorTemplate; 
import com.github.mikephil.charting.utils.ViewPortHandler; 

import java.io.File; 
import java.util.ArrayList; 
import java.util.List; 

public class ChartActivity extends AppCompatActivity { 


    protected BarChart mChart; 
    private String choice; 
    // Storage Permissions 
    private static final int REQUEST_EXTERNAL_STORAGE = 1; 
    private static String[] PERMISSIONS_STORAGE = { 
      Manifest.permission.READ_EXTERNAL_STORAGE, 
      Manifest.permission.WRITE_EXTERNAL_STORAGE 
    }; 



    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_chart); 
     Intent intent = getIntent(); 
     choice = intent.getStringExtra("choice"); 
     Bundle data_bundle = intent.getBundleExtra("bundle"); 
     verifyStoragePermissions(this); 
     mChart = (BarChart) findViewById(R.id.chart); 
     setChart(data_bundle); 
    } 




    private void setChart(Bundle data_bundle) { 


     ArrayList<String> year = (ArrayList<String>) data_bundle.getSerializable("year"); 
     ArrayList<Double> depreciation = (ArrayList<Double>) data_bundle.getSerializable("dep"); 
     ArrayList<Integer> population = (ArrayList<Integer>) data_bundle.getSerializable("pop"); 


     ArrayList<BarEntry> dep_entries = new ArrayList<>(); 
     ArrayList<BarEntry> pop_entries = new ArrayList<>(); 
     for (int i = 0; i< depreciation.size(); i++){ 
      BarEntry entry = new BarEntry(depreciation.get(i).floatValue(),i); 
      dep_entries.add(entry); 
     } 
     for (int i = 0; i< population.size(); i++){ 
      BarEntry entry = new BarEntry(population.get(i).floatValue(), i); 
      pop_entries.add(entry); 
     } 

     BarDataSet barDep = new BarDataSet(pop_entries,"Population"); 
     barDep.setColors(ColorTemplate.COLORFUL_COLORS); 
     BarDataSet barPop = new BarDataSet(dep_entries, "Depreciation"); 
     barDep.setColors(ColorTemplate.JOYFUL_COLORS); 

     List<IBarDataSet> sets = new ArrayList<>(); 
     sets.add(barDep); 
     sets.add(barPop); 

     mChart.setDescription("Bar Chart of data for "+choice); 
     BarData bar_data = new BarData(year, sets); 
     mChart.setData(bar_data); 



    } 

    /** 
    * Checks if the app has permission to write to device storage 
    * 
    * If the app does not has permission then the user will be prompted to grant permissions 
    * 
    * @param activity 
    */ 
    public static void verifyStoragePermissions(Activity activity) { 
     // Check if we have write permission 
     int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE); 

     if (permission != PackageManager.PERMISSION_GRANTED) { 
      // We don't have permission so prompt the user 
      ActivityCompat.requestPermissions(
        activity, 
        PERMISSIONS_STORAGE, 
        REQUEST_EXTERNAL_STORAGE 
      ); 
     } 
    } 


    public void saveChart(View view) { 

     BarChart saveChart = (BarChart) findViewById(R.id.chart); 
     saveChart.saveToGallery("chart",50); 

    } 
}