2016-05-28 26 views
0

我已經聲明變量目錄爲全局變量,並在我的returnData方法螺母中使用該變量,它返回值爲null爲什麼我的方法會返回空值?

public void SaveImage(String FileName, Bitmap mBitmap) { 

    if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { 
     String root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString(); 

     File directory = new File(root + File.separator + "HMS_BARCODE"); 
     directory.mkdirs(); 
     //create a file to write bitmap data 
     File f = new File(directory, FileName + ".png"); 
     Log.e("dir", "" + directory); 
     try { 
      f.createNewFile(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
      Log.e("IOException", "IOException"); 
     } 

     //Convert bitmap to byte array 
     ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
     mBitmap.compress(Bitmap.CompressFormat.PNG, 0, bos); 
     byte[] bytearray = bos.toByteArray(); 

     //Write bytes in file 
     FileOutputStream fos = null; 
     try { 
      fos = new FileOutputStream(f); 
      fos.write(bytearray); 
      fos.flush(); 
      fos.close(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
      Log.e("Exception", "" + e); 
     } 

    } else { 

     String root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString(); 
     directory = new File(root + File.separator + "HMS_BARCODE"); 
     if (!directory.exists()) { 
      directory.mkdirs(); 
     } 

     File f = new File(directory, FileName + ".png"); 
     Log.e("dir1", "" + directory); 



     try { 
      f.createNewFile(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
      Log.e("IOException", "IOException"); 
     } 
     Log.e("dir1", "" + directory); 

     //Convert bitmap to byte array 
     ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
     mBitmap.compress(Bitmap.CompressFormat.PNG, 0, bos); 
     byte[] bytearray = bos.toByteArray(); 

     //Write bytes in file 
     FileOutputStream fos = null; 
     try { 
      fos = new FileOutputStream(f); 
      fos.write(bytearray); 
      fos.flush(); 
      fos.close(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
      Log.e("Exception", "" + e); 
     } 

    } 


} 

// this is method returning null value I want want that directory 
// value to pass to another class 
public File returnData() { 
    Log.e("Exception", "" + directory); 
    return directory; 
} 
+1

我們可以有logcat的? –

+0

因爲https://en.wikipedia.org/wiki/Variable_shadowing – njzk2

回答

1

請正確格式化您的問題,它很難閱讀。從我可以看到你的目錄變量不是全局的,它是本地的saveImage方法。

如果您想要從同一個類實例的不同方法訪問目錄變量,則需要將其聲明爲類變量。例如:

public class MyClass { 

    private File directory; 

    public void saveImage(...) {....} 

    public File returnData(...) {...} 
} 
+0

我已經宣佈它僅作爲全局使用 –

+0

@ShivarajRajagolkar我無法在代碼中看到它,但假設您已全局聲明它,那麼您實際上將它隱藏在saveImage方法,當你做'文件目錄=新文件(根+ File.separator +「HMS_BARCODE」);' –

+0

其實我只是其中一部分.. –

0

必須調用SaveImage方法在returnData方法:

//this is method returning null value i  want want that directory value to  pass to another class 
public File returnData(){ 
    SaveImage(.../*the parameters*/); 
    Log.e("Exception", "" + directory); 
    return directory; 
} 
相關問題