android C程序截屏求助!!!!!!!!!!!

zhwanwslj 2012-05-24 02:48:50
C程序编译可通过,项目运行也可以,但是在执行到调用C程序方法时报错,求助呢,找不到原因了

上代码帮忙看看!!!


java调C:

package com.android.ScreenCap;

import java.io.File;
import android.os.Environment;

public class ScreenCapNative {

static {
System.loadLibrary("screencapjni");
};

private native static void nativeCaptureScreen(String file);

public static String startCaptureScreen() {
String file_name = getUniqueFileName();
if (file_name != null) {
nativeCaptureScreen(file_name);//这里开始调用C程序时就报错了
}
return file_name;
}

private static String getUniqueFileName() {
File file = null;
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
file = createUniqueFile(Environment.getExternalStorageDirectory(),
"ScreenCap.jpg");
}

return (file != null) ? file.getAbsolutePath() : null;

}

private static File createUniqueFile(File directory, String filename) {
File file = new File(directory, filename);
if (!file.exists()) {
return file;
}
// Get the extension of the file, if any.
int index = filename.lastIndexOf('.');
String format;
if (index != -1) {
String name = filename.substring(0, index);
String extension = filename.substring(index);
format = name + "-%d" + extension;
}
else {
format = filename + "-%d.jpg";
}
for (int i = 2; i < Integer.MAX_VALUE; i++) {
file = new File(directory, String.format(format, i));
if (!file.exists()) {
return file;
}
}
return null;
}

}

C程序:


#include <jni.h>
#include <android/log.h>


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <linux/fb.h>
#include <linux/kd.h>

#include <binder/IPCThreadState.h>
#include <binder/ProcessState.h>
#include <binder/IServiceManager.h>

#include <binder/IMemory.h>
#include <surfaceflinger/ISurfaceComposer.h>

#include <SkImageEncoder.h>
#include <SkBitmap.h>



#undef LOG
#define LOG_TAG "logfromc"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)

namespace android {

static void java_com_android_ScreenCap_ScreenCapNative_nativeCaptureScreen(JNIEnv* env,
jobject obj, jstring file)
{
LOGD("into the native screencap");
const char *file_path = env->GetStringUTFChars(file, NULL);

const String16 name("SurfaceFlinger");
sp<ISurfaceComposer> composer;
getService(name, &composer);

sp<IMemoryHeap> heap;
uint32_t w, h;
PixelFormat f;
status_t err = composer->captureScreen(0, &heap, &w, &h, &f, 0, 0);
if (err != NO_ERROR) {
LOGD("screen capture failed: %s\n", strerror(-err));
exit(0);
}
LOGD("before getbase()");
if(heap==NULL){

LOGD("heap is null!");

}
if(w==0){

LOGD("w is 0!");

}
if(h==0){

LOGD("h is 0!");

}

LOGD("screen capture success: w=%u, h=%u, pixels=%p\n",
w, h, heap->getBase());
LOGD("after getbase()");

LOGD("saving file as jpg in %s ...\n", file_path);

SkBitmap b;
b.setConfig(SkBitmap::kARGB_8888_Config, w, h);
b.setPixels(heap->getBase());
SkImageEncoder::EncodeFile(file_path, b,
SkImageEncoder::kJPEG_Type, SkImageEncoder::kDefaultQuality);
}

static JNINativeMethod gScreenCapNativeMethods[] = {
/* name, signature, funcPtr */
{ "nativeCaptureScreen", "(Ljava/lang/String;)V",
(void*) android_ScreenCap_ScreenCapNative_nativeCaptureScreen },
};

int register_android_ScreenCap_ScreenCapNative(JNIEnv* env) {
int res = jniRegisterNativeMethods(env, "com/android/ScreenCap/ScreenCapNative",
gScreenCapNativeMethods, NELEM(gScreenCapNativeMethods));
LOG_FATAL_IF(res < 0, "Unable to register native methods.");

return 0;
}

extern "C" jint JNI_OnLoad(JavaVM* vm, void* reserved)
{
JNIEnv* env = NULL;
jint result = -1;

if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
LOGE("GetEnv failed!");
return result;
}
LOG_ASSERT(env, "Could not retrieve the env!");

register_android_ScreenCap_ScreenCapNative(env);

return JNI_VERSION_1_4;
}

} /* namespace android */



Android.mk文件:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)

LOCAL_MODULE_TAGS := optional

LOCAL_MODULE := libscreencapjni

LOCAL_SRC_FILES := com_android_screencapture_ScreenCapNative.cpp

LOCAL_SHARED_LIBRARIES := \
libcutils \
libutils \
libbinder \
libskia \
libui \
libsurfaceflinger_client \
libandroid_runtime \

LOCAL_C_INCLUDES += \
$(JNI_H_INCLUDE) \
external/skia/include/core \
external/skia/include/effects \
external/skia/include/images \
external/skia/src/ports \
external/skia/include/utils \
frameworks/base/core/jni/android/graphics \

LOCAL_PRELINK_MODULE := false


LOCAL_PATH:= $(call my-dir)

LOCAL_MODULE_TAGS := optional

LOCAL_SRC_FILES := $(call all-subdir-java-files)

LOCAL_PACKAGE_NAME := ScreenCapture
LOCAL_CERTIFICATE := platform

LOCAL_REQUIRED_MODULES := libscreencapjni
LOCAL_JNI_SHARED_LIBRARIES := libscreencapjni

include $(BUILD_SHARED_LIBRARY)

logcat打印的错误为:
05-24 14:49:06.879: ERROR/AndroidRuntime(8311): FATAL EXCEPTION: main
05-24 14:49:06.879: ERROR/AndroidRuntime(8311): java.lang.UnsatisfiedLinkError: nativeCaptureScreen
05-24 14:49:06.879: ERROR/AndroidRuntime(8311): at com.android.ScreenCap.ScreenCapNative.nativeCaptureScreen(Native Method)
05-24 14:49:06.879: ERROR/AndroidRuntime(8311): at com.android.ScreenCap.ScreenCapNative.startCaptureScreen(ScreenCapNative.java:17)
05-24 14:49:06.879: ERROR/AndroidRuntime(8311): at com.android.ScreenCap.ScreenCapjpgActivity$1.onClick(ScreenCapjpgActivity.java:26)
05-24 14:49:06.879: ERROR/AndroidRuntime(8311): at android.view.View.performClick(View.java:2506)
05-24 14:49:06.879: ERROR/AndroidRuntime(8311): at android.view.View$PerformClick.run(View.java:9112)
05-24 14:49:06.879: ERROR/AndroidRuntime(8311): at android.os.Handler.handleCallback(Handler.java:587)
05-24 14:49:06.879: ERROR/AndroidRuntime(8311): at android.os.Handler.dispatchMessage(Handler.java:92)
05-24 14:49:06.879: ERROR/AndroidRuntime(8311): at android.os.Looper.loop(Looper.java:130)
05-24 14:49:06.879: ERROR/AndroidRuntime(8311): at android.app.ActivityThread.main(ActivityThread.java:3835)
05-24 14:49:06.879: ERROR/AndroidRuntime(8311): at java.lang.reflect.Method.invokeNative(Native Method)
05-24 14:49:06.879: ERROR/AndroidRuntime(8311): at java.lang.reflect.Method.invoke(Method.java:507)
05-24 14:49:06.879: ERROR/AndroidRuntime(8311): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
05-24 14:49:06.879: ERROR/AndroidRuntime(8311): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
05-24 14:49:06.879: ERROR/AndroidRuntime(8311): at dalvik.system.NativeStart.main(Native Method)
05-24 14:49:06.889: WARN/ActivityManager(294): Force finishing activity com.android.ScreenCap/.ScreenCapjpgActivity

各位大神求助啊!!!!指点下,可以加QQ交流277143127
...全文
475 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
javakbz 2014-09-09
  • 打赏
  • 举报
回复
楼主,最后怎么解决的?
licongf18 2012-11-22
  • 打赏
  • 举报
回复
楼主解决了吗,我也碰到相同的问题了。。。
hello_kitty8888 2012-10-13
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

在线等待…………
[/Quote]


格式好乱,能给排版下吗?
DrSmart 2012-10-10
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 的回复:]

一般来说可能是JNI库没有打包到APK里,MK文件应该没错,既然你能编译通过。
[/Quote]

有道理,确定打包进去了吗
ttxn1010 2012-10-09
  • 打赏
  • 举报
回复
一般来说可能是JNI库没有打包到APK里,MK文件应该没错,既然你能编译通过。
leer168 2012-05-28
  • 打赏
  • 举报
回复
工程给我,帮你看看
QQ149603158
zhwanwslj 2012-05-28
  • 打赏
  • 举报
回复
函数名字改了也不行 还是那个错 能不能帮忙看下Android.mk配置的对不对 我觉得那里面可能有错
zhwanwslj 2012-05-27
  • 打赏
  • 举报
回复
谢谢 我试试 C\C++实在不会
leer168 2012-05-26
  • 打赏
  • 举报
回复
你这是C++,不是C
命名空间整个android ,起个跟自己相关的吧,这个太。。。。
java.lang.UnsatisfiedLinkError 这是找不到函数,一般是函数名字弄得不一致了

static void java_com_android_ScreenCap_ScreenCapNative_nativeCaptureScreen

你这个方法是 手动拼的吧?

JAVAH 生成出来是这个格式的 ,你试试
JNIEXPORT void JNICALL java_com_android_ScreenCap_ScreenCapNative_nativeCaptureScreen
Tody Guo 2012-05-26
  • 打赏
  • 举报
回复
若我没有猜错的话,这个好像是要root的权限的吧。
zhwanwslj 2012-05-24
  • 打赏
  • 举报
回复
在线等待…………

80,493

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧