简体   繁体   中英

why my method retruniing null value?

I have declared variable directory as a global and using that variable below in my returnData method nut it is returning null value.

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;
}

Please format your question correctly, it is hard to read. From what I can see your directory variable is not global, it is local for saveImage method.

If you want to have access to the directory variable from different methods of the same class instance, then you need to declare it as a class variable. For example:

public class MyClass {

    private File directory;

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

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

You must call SaveImage method in your returnData method :

//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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM