【发布时间】:2017-05-02 01:45:21
【问题描述】:
所以我试图显示相机拍摄的图像并希望稍后通过套接字发送它,但我注意到 onActivityResult() 方法没有被调用,现在导致更多问题。我使用 RESULT_OK,但检查它的值,所以它不是负数。另外,在清单中,我没有限制任何东西。所以我(希望我)基本上检查了常见的错误,仍然无法帮助自己。
主要活动
public class MainActivity extends AppCompatActivity {
ImageView imageView;
Socket clientSocket = null;
byte[] byteFromImage;
Bitmap bitmap;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Thread(new Runnable() {
@Override
public void run() {
try {
clientSocket = new Socket("192.168.178.41", 6066);
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dispatchTakePictureIntent();
new Thread(new Runnable() {
@Override
public void run() {
try {
//mb outputstream
OutputStream output = clientSocket.getOutputStream();
output.write(byteFromImage);
output.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
} }); }
public void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePictureIntent, 1);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1 && resultCode == RESULT_OK) {
Bitmap imageBitmap = (Bitmap) data.getExtras().get("data");
bitmap = imageBitmap;
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Bitmap copy = bitmap;
//maybe jpg
copy.compress(Bitmap.CompressFormat.PNG, 100, stream);
byteFromImage = stream.toByteArray();
imageView.setImageBitmap(imageBitmap);
}
}
public void print(String message) {
Log.e("My output: ",message);
}
}
清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.education.client">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-feature android:name="android.hardware.camera"
android:required="true" />
<application
android:noHistory="true"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
感谢您的帮助!
【问题讨论】:
-
我调用了setResult,现在它在拍照后调用该方法。但是在打开相机后,主要活动崩溃了,因为我在 output.write(byteFromImage); 处从 byteFromImage 获得了一个空指针。这对我来说没有意义,因为该方法是在 onActivityResult() 方法之后调用的
-
这是因为您的位图没有完全转换为字节数组。
-
是的,似乎位图为空,因为 setImageBitmap 给了我一个空指针,这样检索它有什么问题?
标签: java android camera onactivityresult