Java 在android中捕获图像时发生断管错误
我将从android中的一个应用程序中捕获一个图像,但是,输出流存在一些问题,并且JAVA.IO的管道破裂有一个错误。我的代码如下。这里是write命令函数的问题。我已经从git hub下载了这个演示,所以请尽快帮助我。先谢谢你Java 在android中捕获图像时发生断管错误,java,android,file,outputstream,Java,Android,File,Outputstream,我将从android中的一个应用程序中捕获一个图像,但是,输出流存在一些问题,并且JAVA.IO的管道破裂有一个错误。我的代码如下。这里是write命令函数的问题。我已经从git hub下载了这个演示,所以请尽快帮助我。先谢谢你 void takeScreenshot() { String file ="/data/data/com.koushikdutta.screenshot/screenshot.bmp";; OutputStream os = null; int
void takeScreenshot()
{
String file ="/data/data/com.koushikdutta.screenshot/screenshot.bmp";;
OutputStream os = null;
int screenshotUid;
String screenshotUser = null;
Process sh = null;
try
{
try
{
sh = Runtime.getRuntime().exec("su -c sh");
os = sh.getOutputStream();
Log.e("","THE VALUE OF OBJECT IS:::::"+os.toString());
// file ="/data/data/com.koushikdutta.screenshot/screenshot.bmp";
screenshotUid = getUidForPackage("com.koushikdutta.screenshot");
screenshotUser = getUserForPackage("com.koushikdutta.screenshot");
}
catch(Exception e)
{
Log.e("","Hi Error created");
}
try
{
Thread.sleep(2000);
writeCommand(os, "rm "+file);
writeCommand(os, "mkdir /sdcard/dcim");
}
catch(Exception e)
{
Log.e("","Hello How are you??"+e.getMessage());
}
writeCommand(os, "mkdir /sdcard/dcim/Screenshot");
writeCommand(os, "/data/data/com.koushikdutta.screenshot/screenshot");
writeCommand(os, "chown root." + screenshotUser + " " + file);
writeCommand(os, "chmod 660 " + file);
writeCommand(os, "exit");
os.flush();
os.close();
boolean success = false;
for (int i = 0; i < 10; i++)
{
try
{
Thread.sleep(1000);
// if we can successfully get the exit value,
// then that means the process exited.
sh.exitValue();
success = true;
break;
}
catch (Exception ex)
{
Log.e("","Error while"+ex.getMessage());
}
}
try
{
if (!success)
throw new Exception("Unable to take screenshot");
File screenshot = new File(file);
if (!screenshot.exists())
throw new Exception("screenshot.raw file not found!");
mHander.post(new Runnable()
{
public void run()
{
Toast toast = Toast.makeText(
ScreenshotActivity.this,
"Screen captured!", Toast.LENGTH_LONG);
toast.show();
}
});
}
catch(Exception e)
{
Log.e("","ERROR CREATING......."+e.getMessage());
}
try
{
FileInputStream fs = new FileInputStream(file);
mBitmap = BitmapFactory.decodeStream(fs);
mScreenshotFile = String.format(
"/sdcard/dcim/Screenshot/screenshot%d.png",
System.currentTimeMillis());
FileOutputStream fout = new FileOutputStream(mScreenshotFile);
mBitmap.compress(CompressFormat.PNG, 100, fout);
fout.close();
mConnection.scanFile(mScreenshotFile, null);
}
catch (Exception ex)
{
Log.e("","Error while"+ex.getMessage());
}
mHander.post(new Runnable()
{
public void run()
{
mImage.setImageBitmap(mBitmap);
}
});
}
catch (Exception ex)
{
Toast toast = Toast.makeText(
ScreenshotActivity.this, "Error: " + ex.getMessage(),
Toast.LENGTH_LONG);
toast.show();
Log.e("",""+ "Error: " + ex.getMessage());
}
}
static void writeCommand(final OutputStream os, String command)
{
try
{
os.write((command+"\n").getBytes("ASCII"));
}
catch(Exception e)
{
e.printStackTrace();
Log.e("","Error was::::::::::::::::::"+e.getMessage());
}
}
void takeScreenshot()
{
字符串文件=“/data/data/com.koushikdutta.screenshot/screenshot.bmp”;;
OutputStream os=null;
int-screenshotUid;
字符串screenshotUser=null;
进程sh=null;
尝试
{
尝试
{
sh=Runtime.getRuntime().exec(“su-csh”);
os=sh.getOutputStream();
Log.e(“,”对象的值为::“+os.toString());
//file=“/data/data/com.koushikdutta.screenshot/screenshot.bmp”;
screenshotUid=getUidForPackage(“com.koushikdutta.screenshot”);
screenshotUser=getUserForPackage(“com.koushikdutta.screenshot”);
}
捕获(例外e)
{
Log.e(“,”创建了Hi错误“);
}
尝试
{
《睡眠》(2000年);
writeCommand(操作系统,“rm”+文件);
书面命令(os,“mkdir/sdcard/dcim”);
}
捕获(例外e)
{
Log.e(“,”你好吗?“+e.getMessage());
}
写命令(操作系统,“mkdir/sdcard/dcim/Screenshot”);
writeCommand(os,“/data/data/com.koushikdutta.screenshot/screenshot”);
writeCommand(操作系统,“chown root.”+屏幕快照用户+“”+文件);
writeCommand(操作系统,“chmod 660”+文件);
书面命令(os,“退出”);
os.flush();
os.close();
布尔成功=假;
对于(int i=0;i<10;i++)
{
尝试
{
睡眠(1000);
//如果我们能成功获得退出值,
//那就意味着进程退出了。
sh.exitValue();
成功=真实;
打破
}
捕获(例外情况除外)
{
Log.e(“,”错误,而“+ex.getMessage());
}
}
尝试
{
如果(!成功)
抛出新异常(“无法截图”);
文件截图=新文件(文件);
如果(!screenshot.exists())
抛出新异常(“未找到screenshot.raw文件!”);
mHander.post(新Runnable()
{
公开募捐
{
Toast Toast=Toast.makeText(
截屏活动,这个,
“截屏!”,吐司。长度(长);
toast.show();
}
});
}
捕获(例外e)
{
Log.e(“,”创建错误…“+e.getMessage());
}
尝试
{
FileInputStream fs=新的FileInputStream(文件);
mBitmap=BitmapFactory.decodeStream(fs);
mScreenshotFile=String.format(
“/sdcard/dcim/Screenshot/Screenshot%d.png”,
System.currentTimeMillis());
FileOutputStream fout=新的FileOutputStream(mScreenshotFile);
mBitmap.compress(CompressFormat.PNG,100,fout);
fout.close();
mConnection.scanFile(mScreenshotFile,null);
}
捕获(例外情况除外)
{
Log.e(“,”错误,而“+ex.getMessage());
}
mHander.post(新Runnable()
{
公开募捐
{
设置图像位图(mBitmap);
}
});
}
捕获(例外情况除外)
{
Toast Toast=Toast.makeText(
ScreenshotActivity.this,“错误:+ex.getMessage(),
吐司长度(长);
toast.show();
Log.e(“,”+”错误:“+ex.getMessage());
}
}
静态void writeCommand(最终输出流os,字符串命令)
{
尝试
{
os.write((命令+“\n”).getBytes(“ASCII”);
}
捕获(例外e)
{
e、 printStackTrace();
Log.e(“,”错误为:::::::::“+e.getMessage());
}
}
我认为您执行的进程已经终止,因此,输出流
不再可用,因此出现了“断管”错误
我会尝试:
ProcessBuilder builder = new ProcessBuilder("/bin/bash");
builder.redirectErrorStream(true);
Process process = builder.start();
不要直接打电话:
sh = Runtime.getRuntime().exec("su -c sh");
试试看。让我知道
也许你可以从以下方面获得一些好主意:
以防万一。。。
你检查过你的存储卡是否正确了吗?
如果您使用wifi,连接是否良好?我认为存储访问有问题,您应该查看使用权限,也许您应该使用Environment.getExternalStorage.getPath
在要保存映像的位置进行访问。java.io.IOException:运行exec()时出错。命令:[/bin/bash]工作目录:空环境:[ANDROID\u SOCKET\u zygote=10,ANDROID\u BOOTLOGO=1,外部存储=/mnt/sdcard,ANDROID\u ASSETS=/system/app,ASEC\u MOUNTPOINT=/mnt/ASEC,PATH=/sbin:/system/sbin:/system/bin:/system/xbin,ANDROID\u DATA=/DATA,BOOTCLASSPATH=/system/framework/core.jar:/system/framework/ext:/system/framework/ANDROID.policy.jar:/system/frameworkmework/services.jar,ANDROID_PROPERTY_WORKSPACE=932768,ANDROID_ROOT=/system,LD_LIBRARY_PATH=/system/lib]