{“error”: “TypeError: Network request failed”, “status”: “FETCH_ERROR”}错误

问题描述

Android测试机上传图片时报错(iOS正常):
{"error": "TypeError: Network request failed", "status": "FETCH_ERROR"}

问题分析

上传图片使用库是"react-native-image-crop-picker": "^0.39.0"

问题解决

react-native-image-crop-picker返回的image.sourceURL和filename只在iOS下可用,所以代码改成这样:

      const filePath = Platform.OS === 'ios' ? image.sourceURL : image.path;
      const fileName =
        Platform.OS === 'ios' ? image.filename : image.path.split('/').pop();

      let formData = new FormData();
      const file = {
        uri: filePath,
        type: 'multipart/form-data',
        name: fileName,
      };
      formData.append('file', file);

AnimationDrawable导致OOM内存泄露

问题描述:今天做ZTE,发现反复运行,会OOM。

问题解决:Google了如下解决办法,果然有效。

  1. Move code for AnimationDrawable to onResume() method.
  2. Add following code for releasing memory to onPause().
    ad.stop();
    for (int i = 0; i < ad.getNumberOfFrames(); ++i){
        Drawable frame = ad.getFrame(i);
        if (frame instanceof BitmapDrawable) {
            ((BitmapDrawable)frame).getBitmap().recycle();
        }
        frame.setCallback(null);
    }
    ad.setCallback(null);

FFmpeg for Android

开发Make Face,需要合成视频。iOS有官方例子而Android没有,至少4.1以前肯定没有。花了2个礼拜研究,最后找到最捷径的办法:直接FFmpeg命令。

官网:http://ffmpeg.org/index.html

二进制下载:https://bitbucket.org/trovao/ffmpeg-android

参考:https://github.com/guardianproject/android-ffmpeg-java

 

具体步骤:

1.把生成好的二进制可运行的ffmpeg放入res/raw中

2.通过getResources().openRawResource(R.raw.ffmpeg)得到ffmpeg并通过openFileOutput("/data/data/com.couldhll.makeface/files/ffmpeg", Context.MODE_PRIVATE);复制进有权限执行文件的目录
PS:input.mp4和img001.png...img999.png资源文件通过相同方式复制到该目录

3.运行chmod 777 /data/data/com.couldhll.makeface/files/ffmpeg赋予ffmpeg最高权限

4.执行命令
/data/data/com.couldhll.makeface/files/ffmpeg -y -i /data/data/com.couldhll.makeface/files/input.mp4 -vf \"movie=/data/data/com.couldhll.makeface/files/img%03d.png [watermark]; [in][watermark] overlay=10:10 [out]\" /data/data/com.couldhll.makeface/files/output.mp4
把img001.png到img999.png每一帧叠在input.mp4上生成output.mp4

最后,需要注意是生成的时间可能有些久,一般2-3分钟的视频需要40-50秒。因为Android机型性能相差巨大,生成时间不可控。建议把生成视频这部分移到服务器端。
 

Android缺乏手势问题

近来研究Android,发现里面有ScaleGestureDetector,竟然没有RotateGestureDetector。只能搜索到第三方的:

通过Guesture方法实现Rotate
http://code.almeros.com/android-multitouch-gesture-detectors#.Ufc2VRYzMgV
https://github.com/Almeros/android-gesture-detectors

直接再View上实现Rotate
https://github.com/MikeOrtiz/TouchImageView
https://github.com/jasonpolites/gesture-imageview
https://github.com/codepanda-ch/android-gestureimageview

官方的Scale例子
http://developer.android.com/training/gestures/scale.html

Android texture限制问题

问题描述:今天开发Make Face,调试时发现程序突然挂了,也没有报错。看了LogCat,看到如下log:
Bitmap too large to be uploaded into a texture (3264x2448, max=2048x2048)
问题分析:硬件加速的时候,对图片的大小有限制。不同设备可能有不同的最大值。
问题解决:方法有两种
第一种:关闭硬件加速
1.Application级别

<application android:hardwareAccelerated="true" ...>

2.Activity级别

<application android:hardwareAccelerated="true">
    <activity ... />
    <activity android:hardwareAccelerated="false" />
</application>

3.Window级别

getWindow().setFlags(
WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);

4.View级别

myView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

第二种:使用BitmapRegionDecoder加载
http://developer.android.com/reference/android/graphics/BitmapRegionDecoder.html