0

Android携帯に画像を保存しようとしていますが、特定のディレクトリです。どういうわけか私は何時間も費やしましたが、それを機能させることができませんでした。私のコードが機能しない理由、またはそれを行う別の方法があるかどうかを誰かが理解してくれることを願っています。

public function SaveTheImage(me:MouseEvent):void
{       
    ImageSaverBMD.draw(ImageHolder);

    var jpgEncoder:JPGEncoder = new JPGEncoder(100);
    var jpgBytes:ByteArray = jpgEncoder.encode(ImageSaverBMD);

    var myFile:File = File.documentsDirectory.resolvePath("/sdcard / DCIM / Camera/testingimage.jpg");
    var fs:FileStream = new FileStream();
    fs.open(myFile, FileMode.WRITE);
    fs.writeBytes(jpgBytes, 0, jpgBytes.length);
    fs.close();  
}
4

2 に答える 2

0

There are many steps in your algorithm that could cause the bug, with the code you provided I could make a few guesses, but there is a better way.

First of all try this code:

var s:Shape = new Shape();
s.graphics.beginFill(0);
s.graphics.drawCircle( 20, 20, 20);
s.graphics.endFill();

var bd:BitmapData = new BitmapData(40, 40, false);
bd.draw(s);

var jpgEncoder:JPGEncoder = new JPGEncoder(100);
var bytes:ByteArray = jpgEncoder.encode(bd);

var f:File = File.applicationDirectory;
var fs:FileStream = new FileStream();
fs.open(f, FileMode.WRITE);
fs.writeBytes(bytes);
fs.close();

This should save a 80x80 pixels black circle in your provided directory. See if it works. If not: check permissions to have external storage.

If it works, start removing the code block-by-block from top to bottom:

  • Remove the top two blocks and put your ImageSaverBMD.draw(ImageHolder) line. Maybe the problem is with that class? It could be that the bitmap data size was wrong or the matrix for drawing had incorrect translation.

  • The JPGEncoder is probably OK, it's a common used framework, but sometimes when I copy it into my source files I need to change the package in the .as file. Is your package correct?

  • Your URL in the File object has spaces, could that be your problem, try using a simple URL first and see if that works. It could be the problem that you try to navigate to an absolute URL with a relative URL pattern (I could be wrong on this, usually I have different approach of navigating to a folder).

Hope that helps!

于 2013-08-31T01:29:19.947 に答える
-1
private function export():void
{

        var today_date:Date = new Date();
        var thismonth:uint = today_date.getMonth();
        var today_time;
        var currentTime:Date = new Date();
        var minutes = currentTime.getMinutes();
        var seconds = currentTime.getSeconds();
        var hours = currentTime.getHours() * 30 + currentTime.getMinutes() / 2;

        var mnth:Array = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
        var fileName:String = (today_date.getDate()+mnth[thismonth]+today_date.getFullYear()+"_"+currentTime.hours + currentTime.minutes + currentTime.seconds+".png");
        //trace(fileName);// displays current date in United States date format
        var bmd:BitmapData = new BitmapData(board.width, board.height);//(600, 290);

        bmd.draw(board);

        var ba:ByteArray = PNGEncoder.encode(bmd);
       // var file:File = File.applicationDirectory;
        //var file:FileReference = new FileReference();
        var fs : FileStream = new FileStream();
        var targetFile : File = File.documentsDirectory.resolvePath(fileName);
        //var targetFile : File = File.applicationDirectory.resolvePath(fileName);
        fs.open(targetFile, FileMode.WRITE);
        fs.writeBytes(ba);
        fs.close();

        //file.addEventListener(Event.COMPLETE, saveSuccessful);
        saveDialog = new SaveDialog();

        addChild(saveDialog);
        test = setInterval(showMessage,2000);
        //var test = setInterval(showMessage,3000);
        saveDialog.closeBtn.addEventListener(MouseEvent.MOUSE_UP, closeSaveDialog);
        //file.save(ba, fileName);
    }
于 2019-01-16T21:11:00.873 に答える